Skip to content

Commit 1651723

Browse files
committed
Separate lispy from mpcinterface
1 parent 76282f4 commit 1651723

File tree

4 files changed

+42
-35
lines changed

4 files changed

+42
-35
lines changed

mpcinterface/lispy.go renamed to lispy/lispy.go

+23-21
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,36 @@
1-
package mpcinterface
1+
package lispy
22

33
import (
44
"errors"
55
"fmt"
66
"math/big"
77
"strconv"
88
"strings"
9+
10+
mpc "github.com/sunzenshen/golispy/mpcinterface"
911
)
1012

1113
// Lispy is a collection of the Lispy parser definitions
1214
type Lispy struct {
13-
numberParser, operatorParser, exprParser, lispyParser MpcParser
15+
numberParser, operatorParser, exprParser, lispyParser mpc.MpcParser
1416
}
1517

1618
// CleanLispy is used after parsers initiated by InitLispy are not longer to be used
1719
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)
1921
}
2022

2123
// 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)
2527
return num
2628
}
27-
op := getOperator(tree)
28-
x := Eval(getChild(tree, 2))
29+
op := mpc.GetOperator(tree)
30+
x := Eval(mpc.GetChild(tree, 2))
2931
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)))
3234
i++
3335
}
3436
return x
@@ -60,17 +62,17 @@ func evalOp(x int64, op string, y int64) int64 {
6062

6163
// InitLispy returns the parsers for the Lispy language definition
6264
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")
6769
language := "" +
6870
"number : /-?[0-9]+/ ; " +
6971
"operator : '+' | '-' | '*' | '/' | '%' | '^' " +
7072
" | \"add\" | \"sub\" | \"mul\" | \"div\" | \"mod\" | \"pow\" ; " +
7173
"expr : <number> | '(' <operator> <expr>+ ')' ; " +
7274
"lispy : /^/ <operator> <expr>+ /$/ ; "
73-
MpcaLang(language, number, operator, expr, lispy)
75+
mpc.MpcaLang(language, number, operator, expr, lispy)
7476
parserSet := Lispy{}
7577
parserSet.numberParser = number
7678
parserSet.operatorParser = operator
@@ -81,21 +83,21 @@ func InitLispy() Lispy {
8183

8284
// PrintAst prints the AST of a Lispy expression.
8385
func (l *Lispy) PrintAst(input string) {
84-
PrintAst(input, l.lispyParser)
86+
mpc.PrintAst(input, l.lispyParser)
8587
}
8688

8789
// ReadEval takes a string, tries to interpret it in Lispy
8890
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)
9092
if err != nil {
9193
if printErrors {
92-
MpcErrPrint(&r)
94+
mpc.MpcErrPrint(&r)
9395
}
94-
MpcErrDelete(&r)
96+
mpc.MpcErrDelete(&r)
9597
return 0, errors.New("mpc: ReadEval call to MpcParse failed")
9698
}
97-
defer MpcAstDelete(&r)
98-
return Eval(GetOutput(&r)), nil
99+
defer mpc.MpcAstDelete(&r)
100+
return Eval(mpc.GetOutput(&r)), nil
99101
}
100102

101103
// ReadEvalPrint takes a string, tries to interpret it in Lispy, or returns an parsing error

mpcinterface/lispy_test.go renamed to lispy/lispy_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
package mpcinterface
1+
package lispy
22

33
import "testing"
44

55
func TestValidIntegerMath(t *testing.T) {
6-
lispy := InitLispy()
7-
defer CleanLispy(lispy)
6+
l := InitLispy()
7+
defer CleanLispy(l)
88

99
cases := []struct {
1010
input string
@@ -36,7 +36,7 @@ func TestValidIntegerMath(t *testing.T) {
3636
{"- (* 10 10) (+ 1 1 1)", 97},
3737
}
3838
for _, c := range cases {
39-
got, err := lispy.ReadEval(c.input, false)
39+
got, err := l.ReadEval(c.input, false)
4040
if err != nil {
4141
t.Errorf("ReadEval could not parse the following input: \"%s\"", c.input)
4242
} else if got != c.want {

mpcinterface/mpc_interface.go

+11-6
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,28 @@ type MpcParser *C.struct_mpc_parser_t
1717
// MpcResult is a union that returns either an output or error
1818
type MpcResult C.mpc_result_t
1919

20-
func getChild(node MpcAst, index int) MpcAst {
20+
// GetChild accesses the child at a specific index in MpcAst
21+
func GetChild(node MpcAst, index int) MpcAst {
2122
return C.get_child(node, C.int(index))
2223
}
2324

24-
func getContents(node MpcAst) string {
25+
// GetContents accesses the contents of an MpcAst
26+
func GetContents(node MpcAst) string {
2527
return C.GoString(node.contents)
2628
}
2729

28-
func getOperator(node MpcAst) string {
29-
return getContents(getChild(node, 1))
30+
// GetOperator accesses a MpcAst's child node representing an operator
31+
func GetOperator(node MpcAst) string {
32+
return GetContents(GetChild(node, 1))
3033
}
3134

3235
// GetOutput accesses the output field of an input MpcResult
3336
func GetOutput(result *C.mpc_result_t) MpcAst {
3437
return C.get_output(result)
3538
}
3639

37-
func getTag(node MpcAst) string {
40+
// GetTag accesses the tag field of an MpcAst
41+
func GetTag(node MpcAst) string {
3842
return C.GoString(node.tag)
3943
}
4044

@@ -59,7 +63,8 @@ func MpcCleanup(parser1 MpcParser, parser2 MpcParser, parser3 MpcParser, parser4
5963
C.mpc_cleanup_if(C.int(4), parser1, parser2, parser3, parser4)
6064
}
6165

62-
func mpcNew(name string) MpcParser {
66+
// MpcNew returns a pointer to an initiated mpc parser
67+
func MpcNew(name string) MpcParser {
6368
cName := C.CString(name)
6469
defer C.free(unsafe.Pointer(cName))
6570
return C.mpc_new(cName)

prompt.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"fmt"
66
"os"
77

8-
"github.com/sunzenshen/golispy/mpcinterface"
8+
"github.com/sunzenshen/golispy/lispy"
99
)
1010

1111
func main() {
@@ -15,8 +15,8 @@ func main() {
1515
// For reading lines of user input
1616
scanner := bufio.NewScanner(os.Stdin)
1717

18-
lispy := mpcinterface.InitLispy()
19-
defer mpcinterface.CleanLispy(lispy)
18+
l := lispy.InitLispy()
19+
defer lispy.CleanLispy(l)
2020

2121
for {
2222
// Prompt
@@ -25,6 +25,6 @@ func main() {
2525
scanner.Scan()
2626
input := scanner.Text()
2727
// Echo input back to user
28-
lispy.ReadEvalPrint(input)
28+
l.ReadEvalPrint(input)
2929
}
3030
}

0 commit comments

Comments
 (0)