Browse Source

start to use gython objects and instructions

master
Brett Langdon 10 years ago
parent
commit
34185bda45
6 changed files with 68 additions and 24 deletions
  1. +9
    -5
      ast/expression.go
  2. +10
    -7
      compiler/compiler.go
  3. +10
    -2
      compiler/scope.go
  4. +26
    -8
      gython/codeobject.go
  5. +8
    -0
      gython/float.go
  6. +5
    -2
      gython/unicode.go

+ 9
- 5
ast/expression.go View File

@ -1,6 +1,10 @@
package ast package ast
import "fmt"
import (
"fmt"
"github.com/brettlangdon/gython/gython"
)
type Expression interface { type Expression interface {
Node Node
@ -8,13 +12,13 @@ type Expression interface {
} }
type Name struct { type Name struct {
Identifier string
Identifier *gython.Unicode
Context ExpressionContext Context ExpressionContext
} }
func NewName(id string, ctx ExpressionContext) *Name { func NewName(id string, ctx ExpressionContext) *Name {
return &Name{ return &Name{
Identifier: id,
Identifier: gython.NewUnicode([]byte(id)),
Context: ctx, Context: ctx,
} }
} }
@ -26,12 +30,12 @@ func (name *Name) String() string {
} }
type Num struct { type Num struct {
Value int64
Value *gython.Float
} }
func NewNum(i int64) *Num { func NewNum(i int64) *Num {
return &Num{ return &Num{
Value: i,
Value: gython.NewFloat(float64(i)),
} }
} }


+ 10
- 7
compiler/compiler.go View File

@ -45,25 +45,28 @@ func (compiler *Compiler) exitScope() *Scope {
func (compiler *Compiler) assemble(addNone bool) *gython.CodeObject { func (compiler *Compiler) assemble(addNone bool) *gython.CodeObject {
if addNone { 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) codeobject := gython.NewCodeObject([]byte{}, []byte{}, 0)
return codeobject 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 return true
} }
func (compiler *Compiler) visitExpression(expr ast.Expression) bool { func (compiler *Compiler) visitExpression(expr ast.Expression) bool {
switch expr := expr.(type) { switch expr := expr.(type) {
case *ast.Num: case *ast.Num:
compiler.addOp(bytecode.LOAD_CONST)
compiler.addOp(bytecode.LOAD_CONST, expr.Value)
case *ast.Name: case *ast.Name:
compiler.addOp(bytecode.STORE_NAME)
compiler.addOp(bytecode.STORE_NAME, expr.Identifier)
default: default:
fmt.Println(expr) fmt.Println(expr)
} }
@ -78,7 +81,7 @@ func (compiler *Compiler) visitStatement(stmt ast.Statement) bool {
length := len(stmt.Targets) length := len(stmt.Targets)
for i := 0; i < length; i++ { for i := 0; i < length; i++ {
if i < length-1 { if i < length-1 {
compiler.addOp(bytecode.DUP_TOP)
// compiler.addOp(bytecode.DUP_TOP)
} }
compiler.visitExpression(stmt.Targets[i]) compiler.visitExpression(stmt.Targets[i])
} }


+ 10
- 2
compiler/scope.go View File

@ -1,7 +1,15 @@
package compiler package compiler
type Scope struct{}
type Scope struct {
Instructions []*Instruction
}
func NewScope() *Scope { func NewScope() *Scope {
return &Scope{}
return &Scope{
Instructions: make([]*Instruction, 0),
}
}
func (scope *Scope) AddInstruction(instr *Instruction) {
scope.Instructions = append(scope.Instructions, instr)
} }

+ 26
- 8
gython/codeobject.go View File

@ -1,11 +1,11 @@
package gython package gython
type CodeObject struct { 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 Code *Bytes
Constants *Tuple Constants *Tuple
@ -16,12 +16,30 @@ type CodeObject struct {
Filename *Unicode Filename *Unicode
Name *Unicode Name *Unicode
FirstLineNumber int64
FirstLineNumber int
LineNumberTable *Bytes 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() {} func (codeobject *CodeObject) object() {}

+ 8
- 0
gython/float.go View File

@ -3,3 +3,11 @@ package gython
type Float struct { type Float struct {
Value float64 Value float64
} }
func NewFloat(i float64) *Float {
return &Float{
Value: i,
}
}
func (float *Float) object() {}

+ 5
- 2
gython/unicode.go View File

@ -1,10 +1,13 @@
package gython package gython
type Unicode struct { type Unicode struct {
Value []byte
} }
func (unicode *Unicode) object() {} func (unicode *Unicode) object() {}
func NewUnicode() *Unicode {
return &Unicode{}
func NewUnicode(value []byte) *Unicode {
return &Unicode{
Value: value,
}
} }

Loading…
Cancel
Save