Skip to content

Commit

Permalink
icode: Globals and locals with explicit types
Browse files Browse the repository at this point in the history
Closes #78.
  • Loading branch information
JukkaL committed Jan 30, 2013
1 parent e743cf4 commit 6c69012
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
15 changes: 14 additions & 1 deletion icode.py
Expand Up @@ -4,7 +4,8 @@
from nodes import (
FuncDef, IntExpr, MypyFile, ReturnStmt, NameExpr, WhileStmt,
AssignmentStmt, Node, Var, OpExpr, Block, CallExpr, IfStmt, ParenExpr,
UnaryExpr, ExpressionStmt, CoerceExpr, TypeDef, MemberExpr, TypeInfo
UnaryExpr, ExpressionStmt, CoerceExpr, TypeDef, MemberExpr, TypeInfo,
VarDef
)
import nodes
from visitor import NodeVisitor
Expand Down Expand Up @@ -414,6 +415,18 @@ class IcodeBuilder(NodeVisitor<int>):
stmt.accept(self)
return -1
int visit_var_def(self, VarDef d):
assert len(d.items) == 1
var = d.items[0][0]
if d.kind == nodes.LDEF:
reg = self.add_local(var)
if d.init:
self.accept(d.init, reg)
elif d.kind == nodes.GDEF and d.init:
init = self.accept(d.init)
self.add(SetGR(var.full_name(), init))
return -1
int visit_expression_stmt(self, ExpressionStmt s):
self.accept(s.expr)
return -1
Expand Down
24 changes: 24 additions & 0 deletions test/data/cgen-basic.test
Expand Up @@ -105,6 +105,30 @@ void f():
print(j) # 0
f()

[case testLocalWithExplicitType]
class A: pass
void f():
int x = 2
print(x) # 2
int y
print(y) # 0
y = 3
print(y) # 3
A a
print(a) # None
f()

[case testGlobalWithExplicitType]
class A: pass
int x = 2
print(x) # 2
int y
print(y) # 0
y = 3
print(y) # 3
A a
print(a) # None

-- TODO
-- integer overflows and underflows
-- stack overflow
27 changes: 27 additions & 0 deletions test/data/icode-basic.test
Expand Up @@ -357,3 +357,30 @@ L1:
L2:
r1 = None
return r1

[case testLocalWithExplicitType]
void f():
int x = 2
int y
y = 3
A a
[out]
def f:
r0 = 2
r1 = 3
r2 = None
return r2

[case testGlobalWithExplicitType]
class A: pass
int x = 2
int y
y = 3
[out]
def __init:
r0 = 2
__main__.x = r0
r1 = 3
__main__.y = r1
r2 = None
return r2

0 comments on commit 6c69012

Please sign in to comment.