Add support of arrays initialized from litterals
This commit is contained in:
6
a1.gi
Normal file
6
a1.gi
Normal file
@@ -0,0 +1,6 @@
|
||||
package main
|
||||
|
||||
func main() {
|
||||
a := [6]int{1, 2, 3, 4, 5, 6}
|
||||
println(a[1]) // 2
|
||||
}
|
||||
@@ -121,6 +121,8 @@ func (e *Node) Cfg(i *Interpreter) int {
|
||||
n.run = lower
|
||||
case token.SUB:
|
||||
n.run = sub
|
||||
default:
|
||||
panic("missing binary operator function")
|
||||
}
|
||||
maxIndex++
|
||||
n.findex = maxIndex
|
||||
@@ -185,6 +187,18 @@ func (e *Node) Cfg(i *Interpreter) int {
|
||||
n.val = a.Value
|
||||
}
|
||||
|
||||
case *ast.CompositeLit:
|
||||
wireChild(n)
|
||||
n.run = arrayLit
|
||||
maxIndex++
|
||||
n.findex = maxIndex
|
||||
|
||||
case *ast.IndexExpr:
|
||||
wireChild(n)
|
||||
n.run = getIndex
|
||||
maxIndex++
|
||||
n.findex = maxIndex
|
||||
|
||||
case *ast.Ident:
|
||||
// Lookup identifier in frame symbol table. If not found
|
||||
// should check if ident can be defined (assign, param passing...)
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
package interp
|
||||
|
||||
import "fmt"
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
// Run a Go function
|
||||
func Run(def *Node, cf *Frame, args []*Node, rets []int) {
|
||||
@@ -93,6 +96,11 @@ func (interp *Interpreter) call(n *Node, f *Frame) {
|
||||
}
|
||||
}
|
||||
|
||||
func getIndex(n *Node, f *Frame) {
|
||||
a := reflect.ValueOf(value(n.Child[0], f))
|
||||
(*f)[n.findex] = a.Index(int(value(n.Child[1], f).(int64)))
|
||||
}
|
||||
|
||||
func add(n *Node, f *Frame) {
|
||||
(*f)[n.findex] = value(n.Child[0], f).(int64) + value(n.Child[1], f).(int64)
|
||||
}
|
||||
@@ -117,7 +125,7 @@ func lower(n *Node, f *Frame) {
|
||||
(*f)[n.findex] = value(n.Child[0], f).(int64) < value(n.Child[1], f).(int64)
|
||||
}
|
||||
|
||||
func nop(n *Node, i *Frame) {}
|
||||
func nop(n *Node, f *Frame) {}
|
||||
|
||||
func _return(n *Node, f *Frame) {
|
||||
for i, c := range n.Child {
|
||||
@@ -126,3 +134,12 @@ func _return(n *Node, f *Frame) {
|
||||
// FIXME: should be done during compiling, not run
|
||||
n.tnext = nil
|
||||
}
|
||||
|
||||
// create an array of litteral values
|
||||
func arrayLit(n *Node, f *Frame) {
|
||||
a := make([]interface{}, len(n.Child)-1)
|
||||
for i, c := range n.Child[1:] {
|
||||
a[i] = value(c, f)
|
||||
}
|
||||
(*f)[n.findex] = a
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user