@@ -2,100 +2,10 @@ package vibes
22
33import (
44 "context"
5- "errors"
65 "fmt"
76 "strings"
87)
98
10- func (e * Engine ) Compile (source string ) (* Script , error ) {
11- p := newParser (source )
12- program , parseErrors := p .ParseProgram ()
13- if len (parseErrors ) > 0 {
14- return nil , combineErrors (parseErrors )
15- }
16-
17- functions := make (map [string ]* ScriptFunction )
18- classes := make (map [string ]* ClassDef )
19-
20- for _ , stmt := range program .Statements {
21- switch s := stmt .(type ) {
22- case * FunctionStmt :
23- if _ , exists := functions [s .Name ]; exists {
24- return nil , fmt .Errorf ("duplicate function %s" , s .Name )
25- }
26- functions [s .Name ] = & ScriptFunction {Name : s .Name , Params : s .Params , ReturnTy : s .ReturnTy , Body : s .Body , Pos : s .Pos (), Exported : s .Exported , Private : s .Private }
27- case * ClassStmt :
28- if _ , exists := classes [s .Name ]; exists {
29- return nil , fmt .Errorf ("duplicate class %s" , s .Name )
30- }
31- classDef := & ClassDef {
32- Name : s .Name ,
33- Methods : make (map [string ]* ScriptFunction ),
34- ClassMethods : make (map [string ]* ScriptFunction ),
35- ClassVars : make (map [string ]Value ),
36- Body : s .Body ,
37- }
38- for _ , prop := range s .Properties {
39- for _ , name := range prop .Names {
40- if prop .Kind == "property" || prop .Kind == "getter" {
41- getter := & ScriptFunction {
42- Name : name ,
43- Body : []Statement {& ReturnStmt {Value : & IvarExpr {Name : name , position : prop .position }, position : prop .position }},
44- Pos : prop .position ,
45- }
46- classDef .Methods [name ] = getter
47- }
48- if prop .Kind == "property" || prop .Kind == "setter" {
49- setter := & ScriptFunction {
50- Name : name + "=" ,
51- Params : []Param {{
52- Name : "value" ,
53- }},
54- Body : []Statement {
55- & AssignStmt {
56- Target : & IvarExpr {Name : name , position : prop .position },
57- Value : & Identifier {Name : "value" , position : prop .position },
58- position : prop .position ,
59- },
60- & ReturnStmt {Value : & Identifier {Name : "value" , position : prop .position }, position : prop .position },
61- },
62- Pos : prop .position ,
63- }
64- classDef .Methods [name + "=" ] = setter
65- }
66- }
67- }
68- for _ , fn := range s .Methods {
69- classDef .Methods [fn .Name ] = & ScriptFunction {Name : fn .Name , Params : fn .Params , ReturnTy : fn .ReturnTy , Body : fn .Body , Pos : fn .Pos (), Private : fn .Private }
70- }
71- for _ , fn := range s .ClassMethods {
72- classDef .ClassMethods [fn .Name ] = & ScriptFunction {Name : fn .Name , Params : fn .Params , ReturnTy : fn .ReturnTy , Body : fn .Body , Pos : fn .Pos (), Private : fn .Private }
73- }
74- classes [s .Name ] = classDef
75- default :
76- return nil , fmt .Errorf ("unsupported top-level statement %T" , stmt )
77- }
78- }
79-
80- script := & Script {engine : e , functions : functions , classes : classes , source : source }
81- script .bindFunctionOwnership ()
82- return script , nil
83- }
84-
85- func combineErrors (errs []error ) error {
86- if len (errs ) == 1 {
87- return errs [0 ]
88- }
89- msg := ""
90- for _ , err := range errs {
91- if msg != "" {
92- msg += "\n \n "
93- }
94- msg += err .Error ()
95- }
96- return errors .New (msg )
97- }
98-
999func (s * Script ) Call (ctx context.Context , name string , args []Value , opts CallOptions ) (Value , error ) {
10010 if ctx == nil {
10111 ctx = context .Background ()
0 commit comments