vm.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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) []Value
  6. func (handler *Handler) Call(vm *VM, arguments ...Value) []Value {
  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) []Value
  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) []Value {
  38. return vm.CallBuiltin(builtin.Handler, arguments...)
  39. }
  40. // A script defined function
  41. type DefinedValue struct {
  42. CallableValue
  43. Definition *Ast
  44. }
  45. func NewDefinedValue(name string, definition *Ast) 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) []Value {
  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. func (scope *Scope) Register(name string, value Value) Value {
  150. scope.symbols[name] = value
  151. return value
  152. }
  153. // Frame of execution of a function
  154. type Frame struct {
  155. parent *Frame
  156. arguments []Value
  157. results []Value
  158. failed bool
  159. }
  160. func NewFrame(parent *Frame) *Frame {
  161. return &Frame{parent, EmptyValueArray(), EmptyValueArray(), false}
  162. }
  163. // Virtual machine
  164. type VM struct {
  165. TopScope *Scope // Top level scope
  166. TopFrame *Frame // Top level scope
  167. *Scope // Current Scope
  168. *Frame // Current frame
  169. }
  170. func NewVM() *VM {
  171. vm := &VM{NewScope(nil), NewFrame(nil), nil, nil}
  172. vm.Scope = vm.TopScope
  173. vm.Frame = vm.TopFrame
  174. return vm
  175. }
  176. func (vm *VM) PushNewFrame() *Frame {
  177. frame := NewFrame(vm.Frame)
  178. vm.Frame = frame
  179. return frame
  180. }
  181. func (vm *VM) PushNewScope() *Scope {
  182. scope := NewScope(vm.Scope)
  183. vm.Scope = scope
  184. return scope
  185. }
  186. func (vm *VM) PopFrame() *Frame {
  187. if (vm.Frame != vm.TopFrame) && (vm.Frame.parent != nil) {
  188. frame := vm.Frame
  189. vm.Frame = frame.parent
  190. return frame
  191. }
  192. return nil
  193. }
  194. func (vm *VM) PopScope() *Scope {
  195. if (vm.Scope != vm.TopScope) && (vm.Scope.parent != nil) {
  196. scope := vm.Scope
  197. vm.Scope = scope.parent
  198. return scope
  199. }
  200. return nil
  201. }
  202. func (vm *VM) CallDefined(ast *Ast, arguments ...Value) []Value {
  203. return ast.Run(vm, arguments)
  204. }
  205. func (vm *VM) CallBuiltin(handler Handler, arguments ...Value) []Value {
  206. return handler.Call(vm, arguments...)
  207. }
  208. func (vm *VM) CallNamed(name string, arguments ...Value) []Value {
  209. value := vm.Lookup(name)
  210. switch toCall := value.(type) {
  211. case BuiltinValue:
  212. return vm.CallBuiltin(toCall.Handler, arguments...)
  213. case DefinedValue:
  214. return vm.CallDefined(toCall.Definition, arguments...)
  215. default:
  216. return NewValueArray(NewErrorValuef("Cannot call %s: %v", name, value))
  217. }
  218. }
  219. /*
  220. func (vm * VM) CallNamedFramed(name string, arguments ...) []Value {
  221. frame := vm.PushNewFrame()
  222. }
  223. */
  224. func (vm *VM) Register(name string, value Value) Value {
  225. return vm.Scope.Register(name, value)
  226. }
  227. func (vm *VM) RegisterBuiltin(name string, handler Handler) Value {
  228. value := NewBuiltinValue(name, handler)
  229. return vm.Register(name, value)
  230. }
  231. func (vm *VM) RegisterDefined(name string, ast *Ast) Value {
  232. value := NewDefinedValue(name, ast)
  233. return vm.Register(name, value)
  234. }
  235. func (vm *VM) Fail() {
  236. vm.Frame.failed = true
  237. }
  238. func (vm *VM) RunChildren(ast Ast, args ...Value) []Value {
  239. result := NewValueArray()
  240. for _, child := range ast.Children() {
  241. val := child.Run(vm, args)
  242. result = append(result, val...)
  243. }
  244. return result
  245. }
  246. /*
  247. func (vm *VM) RunProgram(ast *BasicAst) ListValue {
  248. return vm.RunChildren(ast, (*VM).RunStatements)
  249. }
  250. func (vm *VM) RunStatements(ast *BasicAst) ListValue {
  251. return vm.RunChildren(ast, (*VM).RunStatement)
  252. }
  253. func (vm *VM) RunStatement(ast *BasicAst) ListValue {
  254. return NewListValue()
  255. }
  256. func (vm *VM) RunSet(ast *BasicAst) ListValue { return NewListValue() }
  257. func (vm *VM) RunGet(ast *BasicAst) ListValue { return NewListValue() }
  258. func (vm *VM) RunTarget(ast *BasicAst) ListValue { return NewListValue() }
  259. func (vm *VM) RunCommand(ast *BasicAst) ListValue { return NewListValue() }
  260. func (vm *VM) RunArguments(ast *BasicAst) ListValue { return NewListValue() }
  261. func (vm *VM) RunArgument(ast *BasicAst) ListValue { return NewListValue() }
  262. func (vm *VM) RunExpression(ast *BasicAst) ListValue { return NewListValue() }
  263. func (vm *VM) RunBlock(ast *BasicAst) ListValue { return NewListValue() }
  264. func (vm *VM) RunParenthesis(ast *BasicAst) ListValue { return NewListValue() }
  265. func (vm *VM) RunList(ast *BasicAst) ListValue { return NewListValue() }
  266. func (vm *VM) RunCapture(ast *BasicAst) ListValue { return NewListValue() }
  267. func (vm *VM) RunWordValue(ast *BasicAst) ListValue { return NewListValue() }
  268. func (vm *VM) RunWord(ast *BasicAst) ListValue { return NewListValue() }
  269. func (vm *VM) RunType(ast *BasicAst) ListValue { return NewListValue() }
  270. func (vm *VM) RunValue(ast *BasicAst) ListValue { return NewListValue() }
  271. func (vm *VM) RunEnd(ast *BasicAst) ListValue { return NewListValue() }
  272. func (vm *VM) RunError(ast *BasicAst) ListValue { return NewListValue() }
  273. func (vm *VM) Run(ast *BasicAst) ListValue {
  274. switch ast.AstKind {
  275. case AstKindProgram:
  276. return vm.RunProgram(ast)
  277. case AstKindStatements:
  278. return vm.RunStatements(ast)
  279. case AstKindStatement:
  280. return vm.RunStatement(ast)
  281. case AstKindSet:
  282. return vm.RunSet(ast)
  283. case AstKindGet:
  284. return vm.RunGet(ast)
  285. case AstKindTarget:
  286. return vm.RunTarget(ast)
  287. case AstKindCommand:
  288. return vm.RunCommand(ast)
  289. case AstKindArguments:
  290. return vm.RunArguments(ast)
  291. case AstKindArgument:
  292. return vm.RunArgument(ast)
  293. case AstKindExpression:
  294. return vm.RunExpression(ast)
  295. case AstKindBlock:
  296. return vm.RunBlock(ast)
  297. case AstKindParenthesis:
  298. return vm.RunParenthesis(ast)
  299. case AstKindList:
  300. return vm.RunList(ast)
  301. case AstKindCapture:
  302. return vm.RunCapture(ast)
  303. case AstKindWordValue:
  304. return vm.RunWordValue(ast)
  305. case AstKindWord:
  306. return vm.RunWord(ast)
  307. case AstKindType:
  308. return vm.RunType(ast)
  309. case AstKindValue:
  310. return vm.RunValue(ast)
  311. case AstKindEnd:
  312. return vm.RunEnd(ast)
  313. case AstKindError:
  314. return vm.RunError(ast)
  315. default:
  316. return ListValue{[]Value{NewErrorValuef("Unknown ast node type: %d", ast.AstKind)}}
  317. }
  318. }
  319. */