Fix bugs that weren't producing expression statements properly
This commit is contained in:
parent
8af24b25a4
commit
0c2a482831
|
@ -19,7 +19,9 @@ type
|
|||
## regarding this function along with
|
||||
## the typed declaration itself
|
||||
decl: TypedFunDecl
|
||||
location: int
|
||||
# The location where the function's code
|
||||
# begins and ends
|
||||
location: tuple[start, stop: int]
|
||||
|
||||
BytecodeGenerator* = ref object
|
||||
## A bytecode generator
|
||||
|
@ -313,7 +315,7 @@ proc handleBuiltinFunction(self: BytecodeGenerator, fn: FunctionWrapper, args: s
|
|||
self.emitByte(PrintInf, line)
|
||||
of Function:
|
||||
self.emitByte(LoadString, line)
|
||||
var loc: string = fn.location.toHex()
|
||||
var loc: string = fn.location.start.toHex()
|
||||
while loc[0] == '0' and loc.len() > 1:
|
||||
loc = loc[1..^1]
|
||||
var str: string
|
||||
|
@ -391,7 +393,12 @@ proc generateLiteral(self: BytecodeGenerator, literal: TypedExpr) =
|
|||
|
||||
proc generateUnary(self: BytecodeGenerator, expression: TypedExpr) =
|
||||
## Emits code for unary expressions
|
||||
echo expression[]
|
||||
discard # TODO
|
||||
|
||||
|
||||
proc generateBinary(self: BytecodeGenerator, expression: TypedExpr) =
|
||||
## Emits code for binary expressions
|
||||
discard # TODO
|
||||
|
||||
|
||||
proc generateExpression(self: BytecodeGenerator, expression: TypedExpr) =
|
||||
|
@ -401,15 +408,14 @@ proc generateExpression(self: BytecodeGenerator, expression: TypedExpr) =
|
|||
else:
|
||||
let node = expression.node
|
||||
case node.kind:
|
||||
of binaryExpr:
|
||||
self.generateUnary(expression)
|
||||
of unaryExpr:
|
||||
discard
|
||||
self.generateUnary(expression)
|
||||
of binaryExpr:
|
||||
self.generateBinary(expression)
|
||||
else:
|
||||
self.error(&"Unknown typed node of type {node.kind} at generateExpression()")
|
||||
|
||||
|
||||
|
||||
proc beginProgram(self: BytecodeGenerator): int =
|
||||
## Emits boilerplate code to set up
|
||||
## a peon program
|
||||
|
@ -441,6 +447,7 @@ proc generate*(self: BytecodeGenerator, compiled: seq[TypedNode], typeChecker: T
|
|||
self.chunk = newChunk()
|
||||
self.typeChecker = typeChecker
|
||||
let offset = self.beginProgram()
|
||||
self.currentFile = typeChecker.getFile()
|
||||
for typedNode in compiled:
|
||||
self.currentNode = typedNode
|
||||
let currentFile = self.currentFile
|
||||
|
|
|
@ -90,6 +90,7 @@ proc addName(self: TypeChecker, name: Name)
|
|||
proc getCurrentNode*(self: TypeChecker): ASTNode = (if self.done(): self.tree[^1] else: self.tree[self.current - 1])
|
||||
proc getCurrentFunction*(self: TypeChecker): Declaration {.inline.} = (if self.currentFunction.isNil(): nil else: self.currentFunction.node)
|
||||
proc getSource*(self: TypeChecker): string {.inline.} = self.source
|
||||
proc getFile*(self: TypeChecker): string {.inline.} = self.file
|
||||
|
||||
|
||||
proc newTypeChecker*: TypeChecker =
|
||||
|
@ -309,7 +310,7 @@ proc toIntrinsic(name: string): Type =
|
|||
return Type(kind: Integer, size: Tiny, signed: true, intrinsic: true)
|
||||
of "uint8":
|
||||
return Type(kind: Integer, size: Tiny, signed: false, intrinsic: true)
|
||||
of "float64":
|
||||
of "float", "float64":
|
||||
return Type(kind: Float, width: Full, intrinsic: true)
|
||||
of "float32":
|
||||
return Type(kind: Float, width: Half, intrinsic: true)
|
||||
|
@ -436,7 +437,6 @@ proc compare(self: TypeChecker, a, b: Type): bool =
|
|||
# have names)
|
||||
if a.name.len() > 0 and b.name.len() > 0:
|
||||
if a.name != b.name:
|
||||
echo "fail: name"
|
||||
return false
|
||||
# Compare fields
|
||||
var hashSet = initHashSet[string]()
|
||||
|
@ -448,15 +448,12 @@ proc compare(self: TypeChecker, a, b: Type): bool =
|
|||
# names
|
||||
for field in hashSet:
|
||||
if field notin a.fields:
|
||||
echo &"fail: {field} notin a.fields"
|
||||
return false
|
||||
if field notin b.fields:
|
||||
echo &"fail: {field} notin b.fields"
|
||||
return false
|
||||
# Ensure fields have matching types
|
||||
for field in hashSet:
|
||||
if not self.compare(a.fields[field], b.fields[field]):
|
||||
echo &"fail: compare(a.{field}, b.{field})"
|
||||
return false
|
||||
hashSet.clear()
|
||||
# Compare generic arguments
|
||||
|
@ -1391,7 +1388,8 @@ proc lentExpr(self: TypeChecker, node: ast.Lent): TypedExpr =
|
|||
proc expression(self: TypeChecker, node: Expression): TypedExpr =
|
||||
## Typechecks expressions
|
||||
if node.isConst():
|
||||
return self.literal(LiteralExpr(node))
|
||||
result = self.literal(LiteralExpr(node))
|
||||
return
|
||||
case node.kind:
|
||||
of callExpr:
|
||||
result = self.call(CallExpr(node))
|
||||
|
|
|
@ -1280,15 +1280,10 @@ proc dispatch(self: Parser): ASTNode =
|
|||
of TokenType.Var, TokenType.Const, TokenType.Let, TokenType.Function,
|
||||
TokenType.Operator, TokenType.Pragma, TokenType.Type:
|
||||
return self.declaration()
|
||||
of TokenType.If, TokenType.Switch, TokenType.Assert, TokenType.While,
|
||||
TokenType.Foreach, TokenType.Break, TokenType.Continue, TokenType.Return,
|
||||
TokenType.Import, TokenType.Export, TokenType.LeftBrace, TokenType.Block:
|
||||
return self.statement()
|
||||
of TokenType.Comment:
|
||||
discard self.step() # TODO
|
||||
else:
|
||||
result = self.expression()
|
||||
self.expect(Semicolon, "expecting semicolon after expression")
|
||||
result = self.statement()
|
||||
|
||||
|
||||
proc findOperators(self: Parser, tokens: seq[Token]) =
|
||||
|
|
Loading…
Reference in New Issue