1
- package mpcinterface
1
+ package lispy
2
2
3
3
import (
4
4
"errors"
5
5
"fmt"
6
6
"math/big"
7
7
"strconv"
8
8
"strings"
9
+
10
+ mpc "github.com/sunzenshen/golispy/mpcinterface"
9
11
)
10
12
11
13
// Lispy is a collection of the Lispy parser definitions
12
14
type Lispy struct {
13
- numberParser , operatorParser , exprParser , lispyParser MpcParser
15
+ numberParser , operatorParser , exprParser , lispyParser mpc. MpcParser
14
16
}
15
17
16
18
// CleanLispy is used after parsers initiated by InitLispy are not longer to be used
17
19
func CleanLispy (l Lispy ) {
18
- MpcCleanup (l .numberParser , l .operatorParser , l .exprParser , l .lispyParser )
20
+ mpc . MpcCleanup (l .numberParser , l .operatorParser , l .exprParser , l .lispyParser )
19
21
}
20
22
21
23
// Eval translates an AST into the final result of the represented instructions
22
- func Eval (tree MpcAst ) int64 {
23
- if strings .Contains (getTag (tree ), "number" ) {
24
- num , _ := strconv .ParseInt (getContents (tree ), 10 , 0 )
24
+ func Eval (tree mpc. MpcAst ) int64 {
25
+ if strings .Contains (mpc . GetTag (tree ), "number" ) {
26
+ num , _ := strconv .ParseInt (mpc . GetContents (tree ), 10 , 0 )
25
27
return num
26
28
}
27
- op := getOperator (tree )
28
- x := Eval (getChild (tree , 2 ))
29
+ op := mpc . GetOperator (tree )
30
+ x := Eval (mpc . GetChild (tree , 2 ))
29
31
i := 3
30
- for strings .Contains (getTag ( getChild (tree , i )), "expr" ) {
31
- x = evalOp (x , op , Eval (getChild (tree , i )))
32
+ for strings .Contains (mpc . GetTag ( mpc . GetChild (tree , i )), "expr" ) {
33
+ x = evalOp (x , op , Eval (mpc . GetChild (tree , i )))
32
34
i ++
33
35
}
34
36
return x
@@ -60,17 +62,17 @@ func evalOp(x int64, op string, y int64) int64 {
60
62
61
63
// InitLispy returns the parsers for the Lispy language definition
62
64
func InitLispy () Lispy {
63
- number := mpcNew ("number" )
64
- operator := mpcNew ("operator" )
65
- expr := mpcNew ("expr" )
66
- lispy := mpcNew ("lispy" )
65
+ number := mpc . MpcNew ("number" )
66
+ operator := mpc . MpcNew ("operator" )
67
+ expr := mpc . MpcNew ("expr" )
68
+ lispy := mpc . MpcNew ("lispy" )
67
69
language := "" +
68
70
"number : /-?[0-9]+/ ; " +
69
71
"operator : '+' | '-' | '*' | '/' | '%' | '^' " +
70
72
" | \" add\" | \" sub\" | \" mul\" | \" div\" | \" mod\" | \" pow\" ; " +
71
73
"expr : <number> | '(' <operator> <expr>+ ')' ; " +
72
74
"lispy : /^/ <operator> <expr>+ /$/ ; "
73
- MpcaLang (language , number , operator , expr , lispy )
75
+ mpc . MpcaLang (language , number , operator , expr , lispy )
74
76
parserSet := Lispy {}
75
77
parserSet .numberParser = number
76
78
parserSet .operatorParser = operator
@@ -81,21 +83,21 @@ func InitLispy() Lispy {
81
83
82
84
// PrintAst prints the AST of a Lispy expression.
83
85
func (l * Lispy ) PrintAst (input string ) {
84
- PrintAst (input , l .lispyParser )
86
+ mpc . PrintAst (input , l .lispyParser )
85
87
}
86
88
87
89
// ReadEval takes a string, tries to interpret it in Lispy
88
90
func (l * Lispy ) ReadEval (input string , printErrors bool ) (int64 , error ) {
89
- r , err := MpcParse (input , l .lispyParser )
91
+ r , err := mpc . MpcParse (input , l .lispyParser )
90
92
if err != nil {
91
93
if printErrors {
92
- MpcErrPrint (& r )
94
+ mpc . MpcErrPrint (& r )
93
95
}
94
- MpcErrDelete (& r )
96
+ mpc . MpcErrDelete (& r )
95
97
return 0 , errors .New ("mpc: ReadEval call to MpcParse failed" )
96
98
}
97
- defer MpcAstDelete (& r )
98
- return Eval (GetOutput (& r )), nil
99
+ defer mpc . MpcAstDelete (& r )
100
+ return Eval (mpc . GetOutput (& r )), nil
99
101
}
100
102
101
103
// ReadEvalPrint takes a string, tries to interpret it in Lispy, or returns an parsing error
0 commit comments