vm.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. // vm, the virtual low level machine that runs MUESLI.
  2. package muesli
  3. // import "fmt"
  4. // Handler function
  5. type Handler func(vm *VM, arguments ...Value) ListValue
  6. func (handler *Handler) Call(vm *VM, arguments ...Value) ListValue {
  7. return (*handler)(vm, arguments...)
  8. }
  9. // A callable Value must implement the Caller interface
  10. type Caller interface {
  11. Call(vm *VM, arguments ...Value) ListValue
  12. }
  13. // Callable value types
  14. type CallableValue struct {
  15. Name string
  16. }
  17. func (val CallableValue) String() string {
  18. return val.Name
  19. }
  20. func (val *CallableValue) Call(vm *VM, arguments ...Value) ListValue {
  21. panic("Not implemented")
  22. }
  23. // A struct to store a built in function
  24. type BuiltinValue struct {
  25. CallableValue
  26. Handler
  27. }
  28. func NewCallableValue(name string) CallableValue {
  29. return CallableValue{name}
  30. }
  31. func NewBuiltinValue(name string, handler Handler) BuiltinValue {
  32. result := BuiltinValue{}
  33. result.Name = name
  34. result.Handler = handler
  35. return result
  36. }
  37. func (builtin *BuiltinValue) Call(vm *VM, arguments ...Value) ListValue {
  38. return vm.CallBuiltin(builtin.Handler, arguments...)
  39. }
  40. // A script defined function
  41. type DefinedValue struct {
  42. CallableValue
  43. Definition *BasicAst
  44. }
  45. func NewDefinedValue(name string, definition *BasicAst) DefinedValue {
  46. result := DefinedValue{}
  47. result.Name = name
  48. result.Definition = definition
  49. return result
  50. }
  51. func (defined *DefinedValue) Call(vm *VM, arguments ...Value) ListValue {
  52. return vm.CallDefined(defined.Definition, arguments...)
  53. }
  54. /*
  55. Amount of types that will be considered inside a signature.
  56. Limited mosty to allow hashability.
  57. */
  58. const TypesInSignature = 8
  59. /* A signature describes the desired types of an overloaded function call. */
  60. type Signature struct {
  61. Types [TypesInSignature]TypeValue
  62. }
  63. func CalculateSignature(arguments ...Value) Signature {
  64. signature := Signature{}
  65. for i := 0; i < cap(signature.Types); i++ {
  66. if i > len(arguments) {
  67. signature.Types[i] = AnyTypeValue
  68. } else {
  69. signature.Types[i] = arguments[i].Type()
  70. }
  71. }
  72. return signature
  73. }
  74. func (signature *Signature) IsMatch(arguments ...Value) bool {
  75. for i, kind := range signature.Types {
  76. if i > len(arguments) || arguments[i] == nil {
  77. return false
  78. }
  79. if kind != arguments[i].Type() && kind != AnyTypeValue {
  80. return false
  81. }
  82. }
  83. return true
  84. }
  85. /* An overload is an overloaded callable value that can be called. */
  86. type Overload struct {
  87. CallableValue
  88. }
  89. /* A cover is a callable that dispatches to other callables depending on
  90. the types of the arguments, in particular the first one. The individual
  91. callable functions are the overloads
  92. */
  93. type CoverValue struct {
  94. CallableValue
  95. Overloads map[Signature]Overload
  96. }
  97. func (cover *CoverValue) Call(vm *VM, arguments ...Value) ListValue {
  98. signature := CalculateSignature(arguments...)
  99. if overload, ok := cover.Overloads[signature]; ok {
  100. return overload.Call(vm, arguments...)
  101. } else {
  102. for signature, overload := range cover.Overloads {
  103. if signature.IsMatch(arguments...) {
  104. return overload.Call(vm, arguments...)
  105. }
  106. }
  107. }
  108. vm.Fail()
  109. return NewListValue(NewErrorValuef("Could not match cover %s with arguments.", cover.Name))
  110. }
  111. const (
  112. CoverTypeValue = TypeValue("Cover")
  113. BuiltinTypeValue = TypeValue("Builtin")
  114. DefinedTypeValue = TypeValue("Defined")
  115. )
  116. func (v CoverValue) Type() TypeValue { return CoverTypeValue }
  117. func (v BuiltinValue) Type() TypeValue { return BuiltinTypeValue }
  118. func (v DefinedValue) Type() TypeValue { return DefinedTypeValue }
  119. // Scope of symbols defined in the VM, hierarchical
  120. type Scope struct {
  121. parent *Scope
  122. children []*Scope
  123. symbols map[string]Value
  124. }
  125. func NewScope(parent *Scope) *Scope {
  126. return &Scope{parent, make([]*Scope, 0), make(map[string]Value)}
  127. }
  128. func (scope *Scope) Parent(level int) *Scope {
  129. if level < 1 {
  130. return scope
  131. }
  132. parent := scope.parent
  133. for parent != nil && level > 1 {
  134. level--
  135. parent = parent.parent
  136. }
  137. return parent
  138. }
  139. func (scope *Scope) Lookup(name string) Value {
  140. value, ok := scope.symbols[name]
  141. if ok {
  142. return value
  143. }
  144. if scope.parent != nil {
  145. return scope.parent.Lookup(name)
  146. }
  147. return NilValue
  148. }
  149. // Frame of execution of a function
  150. type Frame struct {
  151. parent *Frame
  152. arguments ListValue
  153. results ListValue
  154. failed bool
  155. }
  156. func NewFrame(parent *Frame) *Frame {
  157. return &Frame{parent, NewListValue(), NewListValue(), false}
  158. }
  159. // Virtual machine
  160. type VM struct {
  161. TopScope *Scope // Top level scope
  162. TopFrame *Frame // Top level scope
  163. *Scope // Current Scope
  164. *Frame // Current frame
  165. }
  166. func NewVM() *VM {
  167. vm := &VM{NewScope(nil), NewFrame(nil), nil, nil}
  168. vm.Scope = vm.TopScope
  169. vm.Frame = vm.TopFrame
  170. return vm
  171. }
  172. func (vm *VM) CallDefined(ast *BasicAst, arguments ...Value) ListValue {
  173. return NewListValue()
  174. }
  175. func (vm *VM) CallBuiltin(handler Handler, arguments ...Value) ListValue {
  176. return NewListValue()
  177. }
  178. func (vm *VM) Register(name string, value Value) {
  179. }
  180. func (vm *VM) Fail() {
  181. vm.Frame.failed = true
  182. }
  183. func (vm *VM) RunChildren(ast Ast, args ListValue) ListValue {
  184. result := NewListValue()
  185. for _, child := range ast.Children() {
  186. val := child.Run(vm, args)
  187. result.Append(val)
  188. }
  189. return result
  190. }
  191. /*
  192. func (vm *VM) RunProgram(ast *BasicAst) ListValue {
  193. return vm.RunChildren(ast, (*VM).RunStatements)
  194. }
  195. func (vm *VM) RunStatements(ast *BasicAst) ListValue {
  196. return vm.RunChildren(ast, (*VM).RunStatement)
  197. }
  198. func (vm *VM) RunStatement(ast *BasicAst) ListValue {
  199. return NewListValue()
  200. }
  201. func (vm *VM) RunSet(ast *BasicAst) ListValue { return NewListValue() }
  202. func (vm *VM) RunGet(ast *BasicAst) ListValue { return NewListValue() }
  203. func (vm *VM) RunTarget(ast *BasicAst) ListValue { return NewListValue() }
  204. func (vm *VM) RunCommand(ast *BasicAst) ListValue { return NewListValue() }
  205. func (vm *VM) RunArguments(ast *BasicAst) ListValue { return NewListValue() }
  206. func (vm *VM) RunArgument(ast *BasicAst) ListValue { return NewListValue() }
  207. func (vm *VM) RunExpression(ast *BasicAst) ListValue { return NewListValue() }
  208. func (vm *VM) RunBlock(ast *BasicAst) ListValue { return NewListValue() }
  209. func (vm *VM) RunParenthesis(ast *BasicAst) ListValue { return NewListValue() }
  210. func (vm *VM) RunList(ast *BasicAst) ListValue { return NewListValue() }
  211. func (vm *VM) RunCapture(ast *BasicAst) ListValue { return NewListValue() }
  212. func (vm *VM) RunWordValue(ast *BasicAst) ListValue { return NewListValue() }
  213. func (vm *VM) RunWord(ast *BasicAst) ListValue { return NewListValue() }
  214. func (vm *VM) RunType(ast *BasicAst) ListValue { return NewListValue() }
  215. func (vm *VM) RunValue(ast *BasicAst) ListValue { return NewListValue() }
  216. func (vm *VM) RunEnd(ast *BasicAst) ListValue { return NewListValue() }
  217. func (vm *VM) RunError(ast *BasicAst) ListValue { return NewListValue() }
  218. func (vm *VM) Run(ast *BasicAst) ListValue {
  219. switch ast.AstKind {
  220. case AstKindProgram:
  221. return vm.RunProgram(ast)
  222. case AstKindStatements:
  223. return vm.RunStatements(ast)
  224. case AstKindStatement:
  225. return vm.RunStatement(ast)
  226. case AstKindSet:
  227. return vm.RunSet(ast)
  228. case AstKindGet:
  229. return vm.RunGet(ast)
  230. case AstKindTarget:
  231. return vm.RunTarget(ast)
  232. case AstKindCommand:
  233. return vm.RunCommand(ast)
  234. case AstKindArguments:
  235. return vm.RunArguments(ast)
  236. case AstKindArgument:
  237. return vm.RunArgument(ast)
  238. case AstKindExpression:
  239. return vm.RunExpression(ast)
  240. case AstKindBlock:
  241. return vm.RunBlock(ast)
  242. case AstKindParenthesis:
  243. return vm.RunParenthesis(ast)
  244. case AstKindList:
  245. return vm.RunList(ast)
  246. case AstKindCapture:
  247. return vm.RunCapture(ast)
  248. case AstKindWordValue:
  249. return vm.RunWordValue(ast)
  250. case AstKindWord:
  251. return vm.RunWord(ast)
  252. case AstKindType:
  253. return vm.RunType(ast)
  254. case AstKindValue:
  255. return vm.RunValue(ast)
  256. case AstKindEnd:
  257. return vm.RunEnd(ast)
  258. case AstKindError:
  259. return vm.RunError(ast)
  260. default:
  261. return ListValue{[]Value{NewErrorValuef("Unknown ast node type: %d", ast.AstKind)}}
  262. }
  263. }
  264. */