summaryrefslogtreecommitdiff
path: root/parser/compiler.go
diff options
context:
space:
mode:
authorMarc Vertes <mvertes@free.fr>2023-10-13 18:32:13 +0200
committerMarc Vertes <mvertes@free.fr>2023-10-13 18:32:13 +0200
commit36149e5828df1bb35e06905c65f007dd5869aaec (patch)
tree9dcd79d401f7c0dedb62ba33e459765831a10044 /parser/compiler.go
parent40a03025bc1883051c921e6619d751cddcaedeeb (diff)
parser: include absolute paths in symbols
Diffstat (limited to 'parser/compiler.go')
-rw-r--r--parser/compiler.go20
1 files changed, 5 insertions, 15 deletions
diff --git a/parser/compiler.go b/parser/compiler.go
index 68f0596..5d07ae5 100644
--- a/parser/compiler.go
+++ b/parser/compiler.go
@@ -44,12 +44,9 @@ func (c *Compiler) AddSym(name string, value any) int {
func (c *Compiler) Codegen(tokens Tokens) (err error) {
fixList := Tokens{}
- function := []string{""}
-
log.Println("Codegen tokens:", tokens)
for i, t := range tokens {
- scope := function[len(function)-1]
switch t.Id {
case lang.Int:
n, err := strconv.Atoi(t.Str)
@@ -91,16 +88,9 @@ func (c *Compiler) Codegen(tokens Tokens) (err error) {
c.addSym(l, st.Str, nil, symVar, nil, false)
c.Emit(int64(st.Pos), vm.Assign, int64(l))
- case lang.Enter:
- // TODO: merge with label ?
- function = append(function, t.Str[6:])
-
- case lang.Exit:
- function = function[:len(function)-1]
-
case lang.Assign:
st := tokens[i-1]
- s, _, ok := c.getSym(st.Str, scope)
+ s, ok := c.symbols[st.Str]
if !ok {
return fmt.Errorf("symbol not found: %s", st.Str)
}
@@ -120,7 +110,7 @@ func (c *Compiler) Codegen(tokens Tokens) (err error) {
continue
}
}
- s, _, ok := c.getSym(t.Str, scope)
+ s, ok := c.symbols[t.Str]
if !ok {
return fmt.Errorf("symbol not found: %s", t.Str)
}
@@ -132,8 +122,8 @@ func (c *Compiler) Codegen(tokens Tokens) (err error) {
case lang.Label:
// If the label is a function, the symbol already exists
- s, _, ok := c.getSym(t.Str, scope)
lc := len(c.Code)
+ s, ok := c.symbols[t.Str]
if ok {
s.value = lc
if s.kind == symFunc {
@@ -153,7 +143,7 @@ func (c *Compiler) Codegen(tokens Tokens) (err error) {
case lang.JumpFalse:
label := t.Str[10:]
i := 0
- if s, _, ok := c.getSym(label, scope); !ok {
+ if s, ok := c.symbols[label]; !ok {
// t.Beg contains the position in code which needs to be fixed.
t.Beg = len(c.Code)
fixList = append(fixList, t)
@@ -165,7 +155,7 @@ func (c *Compiler) Codegen(tokens Tokens) (err error) {
case lang.Goto:
label := t.Str[5:]
i := 0
- if s, _, ok := c.getSym(label, scope); !ok {
+ if s, ok := c.symbols[label]; !ok {
t.Beg = len(c.Code)
fixList = append(fixList, t)
} else {