Skip to content

Commit

Permalink
stmts
Browse files Browse the repository at this point in the history
  • Loading branch information
covrom committed Sep 13, 2017
1 parent f1b8e7f commit 5cef5f2
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 19 deletions.
7 changes: 5 additions & 2 deletions ast/expr.go
Expand Up @@ -3,19 +3,22 @@ package ast
import (
"strings"

"github.com/covrom/gonec/bincode/binstmt"
"github.com/covrom/gonec/builtins"
"github.com/covrom/gonec/pos"
)

// Expr provides all of interfaces for expression.
type Expr interface {
Pos
pos.Pos
expr()
Simplify() Expr
BinTo(*binstmt.BinStmts, int, *int, bool)
}

// ExprImpl provide commonly implementations for Expr.
type ExprImpl struct {
PosImpl // ExprImpl provide Pos() function.
pos.PosImpl // ExprImpl provide Pos() function.
}

// expr provide restraint interface.
Expand Down
15 changes: 13 additions & 2 deletions ast/stmt.go
@@ -1,15 +1,21 @@
package ast

import (
"github.com/covrom/gonec/bincode/binstmt"
"github.com/covrom/gonec/pos"
)

// Stmt provides all of interfaces for statement.
type Stmt interface {
Pos
pos.Pos
stmt()
Simplify()
BinTo(*binstmt.BinStmts, int, *int)
}

// StmtImpl provide commonly implementations for Stmt..
type StmtImpl struct {
PosImpl // StmtImpl provide Pos() function.
pos.PosImpl // StmtImpl provide Pos() function.
}

// stmt provide restraint interface.
Expand All @@ -25,6 +31,11 @@ func (x *ExprStmt) Simplify() {
x.Expr = x.Expr.Simplify()
}

func (s *ExprStmt) BinTo(bins *binstmt.BinStmts, reg int, lid *int) {
s.Expr.BinTo(bins, reg, lid, true)
// *bins = append(*bins, addBinExpr(s.Expr, reg, lid, true)...)
}

// IfStmt provide "if/else" statement.
type IfStmt struct {
StmtImpl
Expand Down
8 changes: 5 additions & 3 deletions ast/token.go
@@ -1,7 +1,9 @@
package ast

import "github.com/covrom/gonec/pos"

type Token struct {
PosImpl // StmtImpl provide Pos() function.
Tok int
Lit string
pos.PosImpl // StmtImpl provide Pos() function.
Tok int
Lit string
}
6 changes: 4 additions & 2 deletions bincode/bincode.go
Expand Up @@ -4,9 +4,8 @@ import (
"reflect"
"strings"

"github.com/covrom/gonec/env"

"github.com/covrom/gonec/ast"
"github.com/covrom/gonec/env"
)

///////////////////////////////////////////////////////////////
Expand All @@ -20,6 +19,9 @@ func BinaryCode(inast []ast.Stmt, reg int, lid *int) (bcd BinCode) {
bcd.MapLabels()
}()
for _, st := range inast {

st.BinTo(&bins, reg, lid)

// перебираем все подвыражения и команды, и выстраиваем их в линию
// если в команде есть выражение - определяем новый id регистра, присваиваем ему выражение, а в команду передаем id этого регистра
switch s := st.(type) {
Expand Down
18 changes: 9 additions & 9 deletions bincode/binstmt.go → bincode/binstmt/binstmt.go
@@ -1,4 +1,4 @@
package bincode
package binstmt

import (
"compress/gzip"
Expand All @@ -8,19 +8,19 @@ import (
"reflect"
"time"

"github.com/covrom/gonec/ast"
"github.com/covrom/gonec/builtins"
"github.com/covrom/gonec/env"
"github.com/covrom/gonec/pos"
)

type BinStmt interface {
ast.Pos
pos.Pos
binstmt()
SwapId(map[int]int)
}

type BinStmtImpl struct {
ast.PosImpl
pos.PosImpl
fmt.Stringer
}

Expand Down Expand Up @@ -222,14 +222,14 @@ type BinLOAD struct {

func (v *BinLOAD) SwapId(m map[int]int) {
if v.IsId {
if newid, ok := m[v.Val.(int)]; ok {
v.Val = newid
if newid, ok := m[int(v.Val.(core.VMInt))]; ok {
v.Val = core.VMInt(newid)
}
}
}
func (v BinLOAD) String() string {
if v.IsId {
return fmt.Sprintf("LOAD r%d, %#v", v.Reg, env.UniqueNames.Get(v.Val.(int)))
return fmt.Sprintf("LOAD r%d, %#v", v.Reg, env.UniqueNames.Get(int(v.Val.(core.VMInt))))
}
return fmt.Sprintf("LOAD r%d, %#v", v.Reg, v.Val)
}
Expand Down Expand Up @@ -525,11 +525,11 @@ type BinOPER struct {

RegL int // сюда же помещается результат
RegR int
Op int
Op core.VMOperation
}

func (v BinOPER) String() string {
return fmt.Sprintf("OP r%d, %q, r%d", v.RegL, OperMapR[v.Op], v.RegR)
return fmt.Sprintf("OP r%d, %q, r%d", v.RegL, core.OperMapR[v.Op], v.RegR)
}

type BinCALL struct {
Expand Down
2 changes: 1 addition & 1 deletion ast/pos.go → pos/pos.go
@@ -1,4 +1,4 @@
package ast
package pos

// Position provides interface to store code locations.
type Position struct {
Expand Down

0 comments on commit 5cef5f2

Please sign in to comment.