Skip to content
This repository was archived by the owner on Oct 3, 2021. It is now read-only.

Commit 9f4d17b

Browse files
authored
Merge pull request #5 from looplanguage/feat/assign_index
Feat/assign index
2 parents 76ea0a2 + ecd8d89 commit 9f4d17b

4 files changed

Lines changed: 33 additions & 7 deletions

File tree

code/code.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ const (
3535

3636
OpSetVar
3737
OpGetVar
38+
39+
OpSetIndex
3840
)
3941

4042
var definitions = map[OpCode]*Definition{
@@ -68,4 +70,6 @@ var definitions = map[OpCode]*Definition{
6870

6971
OpSetVar: {"OpSetVar", []int{2}},
7072
OpGetVar: {"OpGetVar", []int{2}},
73+
74+
OpSetIndex: {"OpSetIndex", []int{}},
7175
}

compiler/compile.go

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -207,18 +207,40 @@ func (c *Compiler) Compile(node ast.Node, root, identifier, previous string) err
207207
}
208208

209209
c.emit(code.OpSetLocal, s.Index)
210-
return nil
210+
} else {
211+
return fmt.Errorf("undefined variable %s", node.Identifier.Value)
212+
}
213+
} else {
214+
err := c.Compile(node.Value, root, "", previous)
215+
if err != nil {
216+
return err
211217
}
212218

213-
return fmt.Errorf("undefined variable %s", node.Identifier.Value)
219+
c.emit(code.OpSetVar, variable.Index)
214220
}
221+
case *ast.IndexAssign:
222+
// Put the new value on the stack
223+
err := c.Compile(node.Value, root, "", "")
224+
225+
if err != nil {
226+
return err
227+
}
228+
229+
// Put the index on the array
230+
err = c.Compile(node.Index, root, "", "")
231+
232+
if err != nil {
233+
return err
234+
}
235+
236+
// Put the array on the stack
237+
err = c.Compile(node.Object, root, "", "")
215238

216-
err := c.Compile(node.Value, root, "", previous)
217239
if err != nil {
218240
return err
219241
}
220242

221-
c.emit(code.OpSetVar, variable.Index)
243+
c.emit(code.OpSetIndex)
222244
case *ast.Identifier:
223245
variable := c.currentScope.FindByName(node.Value, root)
224246

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ module github.com/looplanguage/compiler
22

33
go 1.17
44

5-
require github.com/looplanguage/loop v0.5.2
5+
require github.com/looplanguage/loop v0.7.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
github.com/looplanguage/loop v0.5.2 h1:ZukH3aO9Khf5PgwQF3L33ehOP7PBhZt+FKmyxe1MB7o=
22
github.com/looplanguage/loop v0.5.2/go.mod h1:H1ENscrP1l2BSqSe4azQ6owr6TkBd7HgaUwDZDeUT6I=
3-
github.com/looplanguage/loop v0.6.0 h1:TWJlbo81V/yWDq7ZW2HZl4bHnApfO6Rs7qjbKI6pNoE=
4-
github.com/looplanguage/loop v0.6.0/go.mod h1:H1ENscrP1l2BSqSe4azQ6owr6TkBd7HgaUwDZDeUT6I=
3+
github.com/looplanguage/loop v0.7.0 h1:60hMnGPXJ0O6xJ/dcrhAlUyplcmZWuTGjW0TTFnTw0M=
4+
github.com/looplanguage/loop v0.7.0/go.mod h1:H1ENscrP1l2BSqSe4azQ6owr6TkBd7HgaUwDZDeUT6I=

0 commit comments

Comments
 (0)