diff --git a/ast/expression.go b/ast/expression.go index 180db0f..7a88c6c 100644 --- a/ast/expression.go +++ b/ast/expression.go @@ -1,6 +1,10 @@ package ast -import "fmt" +import ( + "fmt" + + "github.com/brettlangdon/gython/gython" +) type Expression interface { Node @@ -8,13 +12,13 @@ type Expression interface { } type Name struct { - Identifier string + Identifier *gython.Unicode Context ExpressionContext } func NewName(id string, ctx ExpressionContext) *Name { return &Name{ - Identifier: id, + Identifier: gython.NewUnicode([]byte(id)), Context: ctx, } } @@ -26,12 +30,12 @@ func (name *Name) String() string { } type Num struct { - Value int64 + Value *gython.Float } func NewNum(i int64) *Num { return &Num{ - Value: i, + Value: gython.NewFloat(float64(i)), } } diff --git a/compiler/compiler.go b/compiler/compiler.go index 94cd665..dfd48a9 100644 --- a/compiler/compiler.go +++ b/compiler/compiler.go @@ -45,25 +45,28 @@ func (compiler *Compiler) exitScope() *Scope { func (compiler *Compiler) assemble(addNone bool) *gython.CodeObject { if addNone { - compiler.addOp(bytecode.LOAD_CONST) - compiler.addOp(bytecode.RETURN_VALUE) + // compiler.addOp(bytecode.LOAD_CONST) + // compiler.addOp(bytecode.RETURN_VALUE) } codeobject := gython.NewCodeObject([]byte{}, []byte{}, 0) return codeobject } -func (compiler *Compiler) addOp(op bytecode.Opcode) bool { - fmt.Println(bytecode.Opnames[op]) +func (compiler *Compiler) addOp(op bytecode.Opcode, value gython.Object) bool { + // TODO: add `value` object and get oparg + oparg := 0 + instr := NewInstruction(op, oparg, true) + compiler.currentScope.AddInstruction(instr) return true } func (compiler *Compiler) visitExpression(expr ast.Expression) bool { switch expr := expr.(type) { case *ast.Num: - compiler.addOp(bytecode.LOAD_CONST) + compiler.addOp(bytecode.LOAD_CONST, expr.Value) case *ast.Name: - compiler.addOp(bytecode.STORE_NAME) + compiler.addOp(bytecode.STORE_NAME, expr.Identifier) default: fmt.Println(expr) } @@ -78,7 +81,7 @@ func (compiler *Compiler) visitStatement(stmt ast.Statement) bool { length := len(stmt.Targets) for i := 0; i < length; i++ { if i < length-1 { - compiler.addOp(bytecode.DUP_TOP) + // compiler.addOp(bytecode.DUP_TOP) } compiler.visitExpression(stmt.Targets[i]) } diff --git a/compiler/scope.go b/compiler/scope.go index 13a2415..a6bd9c6 100644 --- a/compiler/scope.go +++ b/compiler/scope.go @@ -1,7 +1,15 @@ package compiler -type Scope struct{} +type Scope struct { + Instructions []*Instruction +} func NewScope() *Scope { - return &Scope{} + return &Scope{ + Instructions: make([]*Instruction, 0), + } +} + +func (scope *Scope) AddInstruction(instr *Instruction) { + scope.Instructions = append(scope.Instructions, instr) } diff --git a/gython/codeobject.go b/gython/codeobject.go index 07f0457..0319d6e 100644 --- a/gython/codeobject.go +++ b/gython/codeobject.go @@ -1,11 +1,11 @@ package gython type CodeObject struct { - ArgCount int64 - KeywordOnlyArgCount int64 - LocalsCount int64 - StackSize int64 - Flags int64 + ArgCount int + KeywordOnlyArgCount int + LocalsCount int + StackSize int + Flags int Code *Bytes Constants *Tuple @@ -16,12 +16,30 @@ type CodeObject struct { Filename *Unicode Name *Unicode - FirstLineNumber int64 + FirstLineNumber int LineNumberTable *Bytes } -func NewCodeObject(filename []byte, name []byte, firstLineNumber int64) *CodeObject { - return &CodeObject{} +func NewCodeObject(filename []byte, name []byte, firstLineNumber int) *CodeObject { + return &CodeObject{ + ArgCount: 0, + KeywordOnlyArgCount: 0, + LocalsCount: 0, + StackSize: 0, + Flags: 0, + + Code: NewBytes(), + Constants: NewTuple(), + Names: NewTuple(), + VariableNames: NewTuple(), + FreeVariableNames: NewTuple(), + CellVariableNames: NewTuple(), + + Filename: NewUnicode(filename), + Name: NewUnicode(name), + FirstLineNumber: firstLineNumber, + LineNumberTable: NewBytes(), + } } func (codeobject *CodeObject) object() {} diff --git a/gython/float.go b/gython/float.go index cffbdf7..7276f21 100644 --- a/gython/float.go +++ b/gython/float.go @@ -3,3 +3,11 @@ package gython type Float struct { Value float64 } + +func NewFloat(i float64) *Float { + return &Float{ + Value: i, + } +} + +func (float *Float) object() {} diff --git a/gython/unicode.go b/gython/unicode.go index 26a15fd..13bf487 100644 --- a/gython/unicode.go +++ b/gython/unicode.go @@ -1,10 +1,13 @@ package gython type Unicode struct { + Value []byte } func (unicode *Unicode) object() {} -func NewUnicode() *Unicode { - return &Unicode{} +func NewUnicode(value []byte) *Unicode { + return &Unicode{ + Value: value, + } }