vm.go 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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) 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 NewCoverValue(name string) CoverValue {
  98. result := CoverValue{}
  99. result.Name = name
  100. result.Overloads = make(map[Signature]Overload)
  101. return result
  102. }
  103. func (cover *CoverValue) Call(vm *VM, arguments ...Value) ListValue {
  104. signature := CalculateSignature(arguments...)
  105. if overload, ok := cover.Overloads[signature]; ok {
  106. return overload.Call(vm, arguments...)
  107. } else {
  108. for signature, overload := range cover.Overloads {
  109. if signature.IsMatch(arguments...) {
  110. return overload.Call(vm, arguments...)
  111. }
  112. }
  113. }
  114. vm.Fail()
  115. return NewListValue(NewErrorValuef("Could not match cover %s with arguments.", cover.Name))
  116. }
  117. const (
  118. CoverTypeValue = TypeValue("Cover")
  119. BuiltinTypeValue = TypeValue("Builtin")
  120. DefinedTypeValue = TypeValue("Defined")
  121. )
  122. func (v CoverValue) Type() TypeValue { return CoverTypeValue }
  123. func (v BuiltinValue) Type() TypeValue { return BuiltinTypeValue }
  124. func (v DefinedValue) Type() TypeValue { return DefinedTypeValue }
  125. // Scope of symbols defined in the VM, hierarchical
  126. type Scope struct {
  127. parent *Scope
  128. children []*Scope
  129. symbols map[string]Value
  130. }
  131. func NewScope(parent *Scope) *Scope {
  132. return &Scope{parent, make([]*Scope, 0), make(map[string]Value)}
  133. }
  134. func (scope *Scope) Parent(level int) *Scope {
  135. if level < 1 {
  136. return scope
  137. }
  138. parent := scope.parent
  139. for parent != nil && level > 1 {
  140. level--
  141. parent = parent.parent
  142. }
  143. return parent
  144. }
  145. func (scope *Scope) Lookup(name string) Value {
  146. value, ok := scope.symbols[name]
  147. if ok {
  148. return value
  149. }
  150. if scope.parent != nil {
  151. return scope.parent.Lookup(name)
  152. }
  153. return NilValue
  154. }
  155. func (scope *Scope) Register(name string, value Value) Value {
  156. scope.symbols[name] = value
  157. return value
  158. }
  159. // Frame of execution of a function
  160. type Frame struct {
  161. parent *Frame
  162. arguments []Value
  163. results []Value
  164. failed bool
  165. }
  166. func NewFrame(parent *Frame) *Frame {
  167. return &Frame{parent, EmptyValueArray(), EmptyValueArray(), false}
  168. }
  169. type Tracer interface {
  170. Trace(vm VM, ast Ast, val ListValue) bool
  171. }
  172. // Virtual machine
  173. type VM struct {
  174. TopScope *Scope // Top level scope
  175. TopFrame *Frame // Top level scope
  176. *Scope // Current Scope
  177. *Frame // Current frame
  178. Tracer // Tracer to emit tracing info to, could be used for logging or debugging
  179. }
  180. func NewVM() *VM {
  181. vm := &VM{NewScope(nil), NewFrame(nil), nil, nil, nil}
  182. vm.Scope = vm.TopScope
  183. vm.Frame = vm.TopFrame
  184. return vm
  185. }
  186. func (vm *VM) PushNewFrame() *Frame {
  187. frame := NewFrame(vm.Frame)
  188. vm.Frame = frame
  189. return frame
  190. }
  191. func (vm *VM) PushNewScope() *Scope {
  192. scope := NewScope(vm.Scope)
  193. vm.Scope = scope
  194. return scope
  195. }
  196. func (vm *VM) PopFrame() *Frame {
  197. if (vm.Frame != vm.TopFrame) && (vm.Frame.parent != nil) {
  198. frame := vm.Frame
  199. vm.Frame = frame.parent
  200. return frame
  201. }
  202. return nil
  203. }
  204. func (vm *VM) PopScope() *Scope {
  205. if (vm.Scope != vm.TopScope) && (vm.Scope.parent != nil) {
  206. scope := vm.Scope
  207. vm.Scope = scope.parent
  208. return scope
  209. }
  210. return nil
  211. }
  212. func (vm *VM) CallDefined(ast *Ast, arguments ...Value) Value {
  213. arr := ast.Run(vm, NewListValue(arguments...))
  214. return arr
  215. }
  216. func (vm *VM) CallBuiltin(handler Handler, arguments ...Value) Value {
  217. return handler.Call(vm, arguments...)
  218. }
  219. func (vm *VM) CallCover(cover CoverValue, arguments ...Value) Value {
  220. return cover.Call(vm, arguments...)
  221. }
  222. func (vm *VM) CallNamed(name string, arguments ...Value) Value {
  223. value := vm.Lookup(name)
  224. switch toCall := value.(type) {
  225. case BuiltinValue:
  226. return vm.CallBuiltin(toCall.Handler, arguments...)
  227. case DefinedValue:
  228. return vm.CallDefined(toCall.Definition, arguments...)
  229. case CoverValue:
  230. return vm.CallCover(toCall, arguments...)
  231. default:
  232. return NewListValue(NewErrorValuef("Cannot call %s: %v", name, value))
  233. }
  234. }
  235. /*
  236. func (vm * VM) CallNamedFramed(name string, arguments ...) []Value {
  237. frame := vm.PushNewFrame()
  238. }
  239. */
  240. func (vm *VM) Register(name string, value Value) Value {
  241. return vm.Scope.Register(name, value)
  242. }
  243. func (vm *VM) RegisterCover(name string) Value {
  244. value := NewCoverValue(name)
  245. return vm.Register(name, value)
  246. }
  247. func (vm *VM) RegisterBuiltin(name string, handler Handler) Value {
  248. value := NewBuiltinValue(name, handler)
  249. return vm.Register(name, value)
  250. }
  251. func (vm *VM) RegisterDefined(name string, ast *Ast) Value {
  252. value := NewDefinedValue(name, ast)
  253. return vm.Register(name, value)
  254. }
  255. func (vm *VM) Fail() {
  256. vm.Frame.failed = true
  257. }
  258. func (vm *VM) RunChildren(ast Ast, args ListValue) Value {
  259. result := NewListValue()
  260. for _, child := range ast.Children() {
  261. val := child.Run(vm, args)
  262. reslist, isList := val.(ListValue)
  263. if isList {
  264. result.AppendList(reslist)
  265. }
  266. result.Append(val)
  267. }
  268. return result
  269. }
  270. func (vm *VM) RunAst(ast Ast, args ListValue) Value {
  271. return ast.Run(vm, args)
  272. }
  273. /*
  274. func (vm *VM) RunProgram(ast *BasicAst) ListValue {
  275. return vm.RunChildren(ast, (*VM).RunStatements)
  276. }
  277. func (vm *VM) RunStatements(ast *BasicAst) ListValue {
  278. return vm.RunChildren(ast, (*VM).RunStatement)
  279. }
  280. func (vm *VM) RunStatement(ast *BasicAst) ListValue {
  281. return NewListValue()
  282. }
  283. func (vm *VM) RunSet(ast *BasicAst) ListValue { return NewListValue() }
  284. func (vm *VM) RunGet(ast *BasicAst) ListValue { return NewListValue() }
  285. func (vm *VM) RunTarget(ast *BasicAst) ListValue { return NewListValue() }
  286. func (vm *VM) RunCommand(ast *BasicAst) ListValue { return NewListValue() }
  287. func (vm *VM) RunArguments(ast *BasicAst) ListValue { return NewListValue() }
  288. func (vm *VM) RunArgument(ast *BasicAst) ListValue { return NewListValue() }
  289. func (vm *VM) RunExpression(ast *BasicAst) ListValue { return NewListValue() }
  290. func (vm *VM) RunBlock(ast *BasicAst) ListValue { return NewListValue() }
  291. func (vm *VM) RunParenthesis(ast *BasicAst) ListValue { return NewListValue() }
  292. func (vm *VM) RunList(ast *BasicAst) ListValue { return NewListValue() }
  293. func (vm *VM) RunCapture(ast *BasicAst) ListValue { return NewListValue() }
  294. func (vm *VM) RunWordValue(ast *BasicAst) ListValue { return NewListValue() }
  295. func (vm *VM) RunWord(ast *BasicAst) ListValue { return NewListValue() }
  296. func (vm *VM) RunType(ast *BasicAst) ListValue { return NewListValue() }
  297. func (vm *VM) RunValue(ast *BasicAst) ListValue { return NewListValue() }
  298. func (vm *VM) RunEnd(ast *BasicAst) ListValue { return NewListValue() }
  299. func (vm *VM) RunError(ast *BasicAst) ListValue { return NewListValue() }
  300. func (vm *VM) Run(ast *BasicAst) ListValue {
  301. switch ast.AstKind {
  302. case AstKindProgram:
  303. return vm.RunProgram(ast)
  304. case AstKindStatements:
  305. return vm.RunStatements(ast)
  306. case AstKindStatement:
  307. return vm.RunStatement(ast)
  308. case AstKindSet:
  309. return vm.RunSet(ast)
  310. case AstKindGet:
  311. return vm.RunGet(ast)
  312. case AstKindTarget:
  313. return vm.RunTarget(ast)
  314. case AstKindCommand:
  315. return vm.RunCommand(ast)
  316. case AstKindArguments:
  317. return vm.RunArguments(ast)
  318. case AstKindArgument:
  319. return vm.RunArgument(ast)
  320. case AstKindExpression:
  321. return vm.RunExpression(ast)
  322. case AstKindBlock:
  323. return vm.RunBlock(ast)
  324. case AstKindParenthesis:
  325. return vm.RunParenthesis(ast)
  326. case AstKindList:
  327. return vm.RunList(ast)
  328. case AstKindCapture:
  329. return vm.RunCapture(ast)
  330. case AstKindWordValue:
  331. return vm.RunWordValue(ast)
  332. case AstKindWord:
  333. return vm.RunWord(ast)
  334. case AstKindType:
  335. return vm.RunType(ast)
  336. case AstKindValue:
  337. return vm.RunValue(ast)
  338. case AstKindEnd:
  339. return vm.RunEnd(ast)
  340. case AstKindError:
  341. return vm.RunError(ast)
  342. default:
  343. return ListValue{[]Value{NewErrorValuef("Unknown ast node type: %d", ast.AstKind)}}
  344. }
  345. }
  346. */