-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
172 lines (161 loc) · 3.86 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
Macaque is a simple interpreter (which will be based off the Python
general purpose programming language) made in Golang.
Right now, all it has is a `print` statement, and variables
using the `var` keyword.
*/
package main
import (
"errors"
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"
)
type Command struct {
Name string
Arguments []interface{}
}
var vars = make(map[string]interface{})
/*
addArgument is convenience method for adding arguments
to a command.
*/
func (c *Command) addArgument(arg interface{}) {
c.Arguments = append(c.Arguments, arg)
}
/*
execute executes a command, by using what's
implied by the command name.
*/
func execute(c *Command) {
switch c.Name {
case "print":
for _, v := range c.Arguments {
fmt.Print(v)
}
fmt.Print("\n")
case "if":
arg := c.Arguments[0]
test := arg.(func() bool)
if ok := test(); ok == true {
fmt.Println("=> true")
} else {
fmt.Println("=> false")
}
}
}
func fail(reason error) {
err := fmt.Errorf("macaque: %v\n", reason)
fmt.Printf("%v", err)
os.Exit(1)
}
/*
lex is a tokenizer that depends on
whitespace for hints
*/
func lex(linum int, line string) {
tokens := strings.Split(line, " ")
switch tokens[0] {
case "print":
cmd := &Command{Name: "print"}
var argument string
if strings.HasPrefix(tokens[1], "'") && strings.HasSuffix(tokens[1], "'") ||
strings.HasPrefix(tokens[1], "\"") && strings.HasSuffix(tokens[1], "\"") {
argument = strings.Join(tokens[1:len(tokens)-1], " ")
}
if v, ok := vars[tokens[1]]; ok == true {
switch v.(type) {
case string:
argument = v.(string)
case int:
argument = strconv.Itoa(v.(int))
default:
err := fmt.Sprintf("line %v: variable %s has unknown type or not declared",
linum, tokens[1])
fail(errors.New(err))
}
}
cmd.addArgument(argument)
execute(cmd)
case "var":
arg := strings.Join(tokens[3:], " ")
name := tokens[1]
// check if value is an int
new_arg, err := strconv.Atoi(arg)
if err != nil {
// int conversion failed, let's try bool
new_arg, err := strconv.ParseBool(arg)
if err != nil {
// bool conversion failed, so value is a string
vars[name] = arg
} else {
vars[name] = new_arg
}
} else {
vars[name] = new_arg
}
case "if":
if strings.HasPrefix(tokens[1], "(") {
if strings.HasSuffix(tokens[1], ")") {
cmd := &Command{Name: "if"}
// the test is usually about a variable
if len(strings.TrimSuffix(strings.TrimPrefix(tokens[1], "("), ")")) == 0 ||
strings.TrimSuffix(strings.TrimPrefix(tokens[1], "("), ")") == "true" ||
strings.TrimSuffix(strings.TrimPrefix(tokens[1], "("), ")") == "false" {
fail(errors.New(fmt.Sprintf("line %d: pointless to evaluate the \"if\"", linum)))
}
test := strings.TrimSuffix(strings.TrimPrefix(tokens[1], "("), ")")
v := vars[test]
switch v.(type) {
case int:
cmd.addArgument(func() bool {
if v.(int) >= 1 {
return true
} else {
return false
}
})
case string:
cmd.addArgument(func() bool {
if len(v.(string)) == 2 {
return false
} else {
return true
}
})
case bool:
cmd.addArgument(func() bool {
return v.(bool)
})
}
execute(cmd)
} else {
err := fmt.Sprintf("line %v: missing ending parenthesis", linum)
fail(errors.New(err))
}
} else {
err := fmt.Sprintf("line %v: test unparenthesized", linum)
fail(errors.New(err))
}
default:
err := fmt.Sprintf("line %v: unknown identifer %s", linum, tokens[0])
fail(errors.New(err))
}
}
func main() {
if len(os.Args) == 1 {
fail(errors.New("program name not provided"))
}
progname := os.Args[1]
file, err := ioutil.ReadFile(progname)
if err != nil {
fail(err)
}
program := strings.Split(string(file), "\n")
// iterate over the lines in the program
for linenum, line := range program {
lex(linenum, line)
}
}