Skip to content

Commit

Permalink
codegen: use / for float and // for int
Browse files Browse the repository at this point in the history
  • Loading branch information
StunxFS committed Jul 2, 2024
1 parent 4634e8a commit 521adfa
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 6 deletions.
19 changes: 19 additions & 0 deletions bsc/AST.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ class ParExpr(Expr):
def __init__(self, expr, pos):
self.expr = expr
self.pos = pos
self.typ = None

def __str__(self):
return f"({self.expr})"
Expand All @@ -192,6 +193,7 @@ class BuiltinVar(Expr):
def __init__(self, name, pos):
self.name = name
self.pos = pos
self.typ = None

def __str__(self):
return f"${self.name}"
Expand All @@ -202,6 +204,7 @@ def __repr__(self):
class NilLiteral(Expr):
def __init__(self, pos):
self.pos = pos
self.typ = None

def __str__(self):
return "nil"
Expand All @@ -213,6 +216,7 @@ class BoolLiteral(Expr):
def __init__(self, value, pos):
self.value = value
self.pos = pos
self.typ = None

def __str__(self):
return str(self.value)
Expand All @@ -224,6 +228,7 @@ class NumberLiteral(Expr):
def __init__(self, value, pos):
self.value = value
self.pos = pos
self.typ = None

def __str__(self):
return self.value
Expand All @@ -235,6 +240,7 @@ class StringLiteral(Expr):
def __init__(self, value, pos):
self.value = value
self.pos = pos
self.typ = None

def __str__(self):
return self.value
Expand All @@ -245,6 +251,7 @@ def __repr__(self):
class SelfLiteral(Expr):
def __init__(self, pos):
self.pos = pos
self.typ = None

def __str__(self):
return "self"
Expand All @@ -257,6 +264,7 @@ def __init__(self, elems, is_fixed, pos):
self.elems = elems
self.is_fixed = is_fixed
self.pos = pos
self.typ = None

def __str__(self):
_str = "#[" if self.is_fixed else "["
Expand All @@ -269,6 +277,7 @@ class TupleLiteral(Expr):
def __init__(self, elems, pos):
self.elems = elems
self.pos = pos
self.typ = None

def __str__(self):
return f"[{', '.join([str(elem) for elem in self.elems])}]"
Expand All @@ -280,6 +289,7 @@ class EnumLiteral(Expr):
def __init__(self, name, pos):
self.name = name
self.pos = pos
self.typ = None

def __str__(self):
return f".{self.name}"
Expand All @@ -292,6 +302,7 @@ def __init__(self, name, pos):
self.name = name
self.pos = pos
self.sym = None
self.typ = None

def __str__(self):
return self.name
Expand All @@ -304,6 +315,7 @@ def __init__(self, left, name, pos):
self.left = left
self.name = name
self.pos = pos
self.typ = None

def __str__(self):
return f"{self.left}::{self.name}"
Expand All @@ -317,6 +329,7 @@ def __init__(self, left, name, pos):
self.name = name
self.pos = pos
self.sym = None
self.typ = None

def __str__(self):
return f"{self.left}.{self.name}"
Expand All @@ -329,6 +342,7 @@ def __init__(self, left, args, pos):
self.left = left
self.args = args
self.pos = pos
self.typ = None

def __str__(self):
return f"{self.left}({', '.join([str(arg) for arg in self.args])})"
Expand Down Expand Up @@ -470,6 +484,7 @@ class IfExpr(Expr):
def __init__(self, branches, pos):
self.branches = branches
self.pos = pos
self.typ = None

def __str__(self):
s = ""
Expand All @@ -491,12 +506,14 @@ def __init__(self, cond, is_else, expr, pos):
self.is_else = is_else
self.expr = expr
self.pos = pos
self.typ = None

class MatchExpr(Expr):
def __init__(self, expr, branches, pos):
self.expr = expr
self.branches = branches
self.pos = pos
self.typ = None

def __str__(self):
if self.expr:
Expand All @@ -522,6 +539,7 @@ def __init__(self, cases, is_else, stmt, pos):
self.is_else = is_else
self.stmt = stmt
self.pos = pos
self.typ = None

class BlockExpr(Expr):
def __init__(self, is_unsafe, stmts, expr, pos):
Expand All @@ -541,6 +559,7 @@ class ReturnExpr(Expr):
def __init__(self, expr, pos):
self.expr = expr
self.pos = pos
self.typ = None

def __str__(self):
if self.expr != None:
Expand Down
18 changes: 13 additions & 5 deletions bsc/codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ def gen_expr(self, expr):
elif isinstance(expr, BoolLiteral):
return LuaBooleanLit("true" if expr.value else "false")
elif isinstance(expr, NumberLiteral):
return LuaNumberLit(expr.value)
return LuaNumberLit(expr.value, expr.typ == self.ctx.float_type)
elif isinstance(expr, UnaryExpr):
right = self.gen_expr(expr.right)
if isinstance(right, LuaBooleanLit) and expr.op == UnaryOp.bang:
Expand All @@ -162,12 +162,15 @@ def gen_expr(self, expr):
return LuaNumberLit(str(~int(right.value)))
return LuaUnaryExpr(expr.op.to_lua_op(), right)
elif isinstance(expr, BinaryExpr):
op = expr.op.to_lua_op()
left = self.gen_expr(expr.left)
right = self.gen_expr(expr.right)
if isinstance(left,
LuaNumberLit) and isinstance(right, LuaNumberLit):
leftn = int(left.value)
rightn = int(right.value)
leftn = float(left.value) if left.is_float else int(left.value)
rightn = float(right.value
) if left.is_float else int(right.value)
if left.is_float and op == "/": op = "//"
match expr.op:
case BinaryOp.plus:
return LuaNumberLit(str(leftn + rightn))
Expand All @@ -176,7 +179,12 @@ def gen_expr(self, expr):
case BinaryOp.mul:
return LuaNumberLit(str(leftn * rightn))
case BinaryOp.div:
return LuaNumberLit(str(leftn // rightn))
return LuaNumberLit(
str(
leftn / rightn if left.is_float else leftn //
rightn
), left.is_float
)
case BinaryOp.mod:
return LuaNumberLit(str(leftn % rightn))
case BinaryOp.bit_and:
Expand Down Expand Up @@ -210,7 +218,7 @@ def gen_expr(self, expr):
return LuaBooleanLit(leftb and rightb)
case BinaryOp.logical_or:
return LuaBooleanLit(leftb or rightb)
return LuaBinaryExpr(left, expr.op.to_lua_op(), right)
return LuaBinaryExpr(left, op, right)
elif isinstance(expr, Ident):
if isinstance(
expr.sym, Object
Expand Down
5 changes: 4 additions & 1 deletion bsc/sema.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ def check_expr(self, expr):
expr.typ = self.ctx.bool_type
elif isinstance(expr, NumberLiteral):
if any(ch in expr.value for ch in [".", "e", "E"]
) and expr.value[:2].lower() not in ['0x', '0o', '0b']:
) and (expr.value[:2].lower() not in ['0x', '0o', '0b']):
expr.typ = self.ctx.float_type
else:
expr.typ = self.ctx.int_type
Expand Down Expand Up @@ -246,6 +246,9 @@ def check_expr(self, expr):
expr.pos,
["operator `~` is only defined for type `int`"]
)
elif isinstance(expr, BinaryExpr):
self.check_expr(expr.left)
self.check_expr(expr.right)
else:
expr.typ = self.ctx.void_type # tmp
return expr.typ
Expand Down
3 changes: 3 additions & 0 deletions tests/main.bs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ const bin_num = 0b10101;
const octal_num = 0o666;
const float_num = 0.5e4;

const f = 1.0 / 2.0;
const i = 1 / 2;

fn main() void {
print("hello");
const XXX = dec_num;
Expand Down

0 comments on commit 521adfa

Please sign in to comment.