2022-04-04 12:29:23 +02:00
# Copyright 2022 Mattia Giambirtone & All Contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import meta / token
import meta / ast
import meta / errors
import .. / config
import .. / util / multibyte
import strformat
import algorithm
import parseutils
2022-04-21 15:25:29 +02:00
import strutils
2022-05-07 10:48:01 +02:00
import sequtils
2022-05-22 13:02:48 +02:00
import os
2022-04-04 12:29:23 +02:00
export ast
export token
export multibyte
2022-05-22 15:26:12 +02:00
2022-05-07 10:48:01 +02:00
type
2022-05-29 15:54:01 +02:00
TypeKind = enum
2022-05-07 10:48:01 +02:00
## An enumeration of compile-time
## types
Int8 , UInt8 , Int16 , UInt16 , Int32 ,
UInt32 , Int64 , UInt64 , Float32 , Float64 ,
Char , Byte , String , Function , CustomType ,
2022-05-24 09:55:08 +02:00
Nil , Nan , Bool , Inf , Typedesc , Generic ,
2022-05-29 15:54:01 +02:00
Mutable , Reference , Pointer
2022-05-24 09:55:08 +02:00
Any # Any is used internally in a few cases,
# for example when looking for operators
# when only the type of the arguments is of
# interest
2022-05-29 15:54:01 +02:00
Type = ref object
2022-05-07 10:48:01 +02:00
## A wrapper around
## compile-time types
2022-05-29 15:54:01 +02:00
case kind : TypeKind :
2022-05-07 10:48:01 +02:00
of Function :
2022-05-29 15:54:01 +02:00
name : string
2022-05-27 14:01:57 +02:00
isLambda : bool
2022-05-29 17:04:19 +02:00
isGenerator : bool
isCoroutine : bool
2022-05-29 15:54:01 +02:00
args : seq [ tuple [ name : string , kind : Type ] ]
returnType : Type
of Mutable , Reference , Pointer :
value : Type
2022-05-07 10:48:01 +02:00
else :
discard
2022-05-22 13:02:48 +02:00
2022-05-07 10:48:01 +02:00
# This way we don't have recursive dependency issues
import meta / bytecode
export bytecode
2022-04-04 12:29:23 +02:00
type
Name = ref object
## A compile-time wrapper around
2022-05-04 14:27:15 +02:00
## statically resolved names
2022-05-07 10:48:01 +02:00
# Name of the identifier
name : IdentExpr
# Owner of the identifier (module)
owner : string
# Scope depth
depth : int
# Is this name private?
isPrivate : bool
# Is this a constant?
isConst : bool
# Can this name's value be mutated?
isLet : bool
# The name's type
valueType : Type
# For functions, this marks where the function's
2022-05-29 15:54:01 +02:00
# code begins. For variables, this stores where
# their StoreVar/StoreHeap instruction was emitted
2022-05-07 10:48:01 +02:00
codePos : int
2022-05-29 15:54:01 +02:00
# Is the name closed over (i.e. used in a closure)?
2022-05-27 14:01:57 +02:00
isClosedOver : bool
2022-05-29 15:54:01 +02:00
# Where is this node declared in the file?
line : int
2022-04-04 12:29:23 +02:00
Loop = object
## A "loop object" used
## by the compiler to emit
## appropriate jump offsets
## for continue and break
## statements
2022-05-07 10:48:01 +02:00
# Position in the bytecode where the loop starts
start : int
# Scope depth where the loop is located
depth : int
# Absolute jump offsets into our bytecode that we need to
# patch. Used for break statements
breakPos : seq [ int ]
2022-04-04 12:29:23 +02:00
Compiler * = ref object
2022-05-04 14:27:15 +02:00
## A wrapper around the Peon compiler's state
2022-04-04 12:29:23 +02:00
# The bytecode chunk where we write code to
chunk : Chunk
# The output of our parser (AST)
2022-05-18 13:32:32 +02:00
ast : seq [ Declaration ]
2022-04-04 12:29:23 +02:00
# The current AST node we're looking at
current : int
# The current file being compiled (used only for
# error reporting)
file : string
# Compile-time "simulation" of the stack at
# runtime to load variables that have stack
# behavior more efficiently
names : seq [ Name ]
2022-05-23 10:49:38 +02:00
# Beginning of stack frames for function calls
frames : seq [ int ]
2022-04-04 12:29:23 +02:00
# The current scope depth. If > 0, we're
# in a local scope, otherwise it's global
scopeDepth : int
# The current function being compiled
currentFunction : FunDecl
# Are optimizations turned on?
2022-05-29 15:54:01 +02:00
enableOptimizations : bool
2022-04-04 12:29:23 +02:00
# The current loop being compiled (used to
# keep track of where to jump)
currentLoop : Loop
# The current module being compiled
# (used to restrict access to statically
# defined variables at compile time)
currentModule : string
# Each time a defer statement is
# compiled, its code is emitted
# here. Later, if there is any code
# to defer in the current function,
# funDecl will wrap the function's code
# inside an implicit try/finally block
# and add this code in the finally branch.
# This sequence is emptied each time a
2022-05-20 15:47:04 +02:00
# function declaration is compiled and stores only
2022-04-04 12:29:23 +02:00
# deferred code for the current function (may
# be empty)
deferred : seq [ uint8 ]
# List of closed-over variables
2022-05-26 18:31:40 +02:00
closedOver : seq [ Name ]
2022-04-04 12:29:23 +02:00
2022-05-29 17:04:19 +02:00
proc `$` ( self : Name ) : string =
## Stringifies a name object
result & = & " Name(name= ' {self.name} ' , depth={self.depth}, owner= ' {self.owner} ' , private={self.isPrivate}, let={self.isLet}, const={self.isConst} "
result & = & " , pos={self.codePos}, closure={self.isClosedOver}, line={self.line}) "
2022-05-22 17:23:52 +02:00
2022-04-11 14:41:20 +02:00
proc newCompiler * ( enableOptimizations : bool = true ) : Compiler =
2022-04-04 12:29:23 +02:00
## Initializes a new Compiler object
new ( result )
result . ast = @ [ ]
result . current = 0
result . file = " "
result . names = @ [ ]
result . scopeDepth = 0
result . currentFunction = nil
result . enableOptimizations = enableOptimizations
result . currentModule = " "
2022-05-23 10:49:38 +02:00
result . frames = @ [ ]
2022-04-04 12:29:23 +02:00
## Forward declarations
2022-05-04 14:27:15 +02:00
proc expression ( self : Compiler , node : Expression )
proc statement ( self : Compiler , node : Statement )
proc declaration ( self : Compiler , node : Declaration )
2022-04-04 12:29:23 +02:00
proc peek ( self : Compiler , distance : int = 0 ) : ASTNode
2022-04-21 15:25:29 +02:00
proc identifier ( self : Compiler , node : IdentExpr )
proc varDecl ( self : Compiler , node : VarDecl )
2022-05-04 14:27:15 +02:00
proc inferType ( self : Compiler , node : LiteralExpr ) : Type
proc inferType ( self : Compiler , node : Expression ) : Type
2022-05-22 13:02:48 +02:00
proc findByName ( self : Compiler , name : string ) : seq [ Name ]
proc findByType ( self : Compiler , name : string , kind : Type ) : seq [ Name ]
2022-05-22 15:26:12 +02:00
proc compareTypes ( self : Compiler , a , b : Type ) : bool
2022-05-22 17:23:52 +02:00
proc patchReturnAddress ( self : Compiler , retAddr : int )
2022-04-04 12:29:23 +02:00
## End of forward declarations
2022-04-26 09:29:59 +02:00
## Public getter for nicer error formatting
2022-04-04 12:29:23 +02:00
proc getCurrentNode * ( self : Compiler ) : ASTNode = ( if self . current > =
self . ast . len ( ) : self . ast [ ^ 1 ] else : self . ast [ self . current - 1 ] )
2022-05-22 11:49:38 +02:00
proc getCurrentFunction * ( self : Compiler ) : Declaration {. inline . } = self . currentFunction
2022-05-26 18:31:40 +02:00
proc getFile * ( self : Compiler ) : string {. inline . } = self . file
proc getModule * ( self : Compiler ) : string {. inline . } = self . currentModule
2022-04-04 12:29:23 +02:00
## Utility functions
proc peek ( self : Compiler , distance : int = 0 ) : ASTNode =
## Peeks at the AST node at the given distance.
## If the distance is out of bounds, the last
## AST node in the tree is returned. A negative
## distance may be used to retrieve previously
## consumed AST nodes
if self . ast . high ( ) = = - 1 or self . current + distance > self . ast . high ( ) or
self . current + distance < 0 :
result = self . ast [ ^ 1 ]
else :
result = self . ast [ self . current + distance ]
proc done ( self : Compiler ) : bool =
## Returns true if the compiler is done
## compiling, false otherwise
result = self . current > self . ast . high ( )
2022-05-22 11:49:38 +02:00
proc error ( self : Compiler , message : string ) {. raises : [ CompileError ] . } =
## Raises a CompileError exception
2022-05-22 13:02:48 +02:00
raise CompileError ( msg : message , node : self . getCurrentNode ( ) , file : self . file , module : self . currentModule )
2022-04-04 12:29:23 +02:00
proc step ( self : Compiler ) : ASTNode =
## Steps to the next node and returns
## the consumed one
result = self . peek ( )
if not self . done ( ) :
self . current + = 1
2022-05-22 13:02:48 +02:00
proc emitByte ( self : Compiler , byt : OpCode | uint8 ) =
2022-04-04 12:29:23 +02:00
## Emits a single byte, writing it to
## the current chunk being compiled
when DEBUG_TRACE_COMPILER :
echo & " DEBUG - Compiler: Emitting { $byt } "
self . chunk . write ( uint8 byt , self . peek ( ) . token . line )
2022-05-22 13:02:48 +02:00
proc emitBytes ( self : Compiler , bytarr : openarray [ OpCode | uint8 ] ) =
## Handy helper method to write arbitrary bytes into
2022-04-04 12:29:23 +02:00
## the current chunk, calling emitByte on each of its
## elements
2022-05-07 10:48:01 +02:00
for b in bytarr :
self . emitByte ( b )
2022-04-04 12:29:23 +02:00
2022-05-20 15:47:04 +02:00
proc makeConstant ( self : Compiler , val : Expression , typ : Type ) : array [ 3 , uint8 ] =
2022-04-04 12:29:23 +02:00
## Adds a constant to the current chunk's constant table
## and returns its index as a 3-byte array of uint8s
2022-05-22 15:26:12 +02:00
var v : int
discard parseInt ( val . token . lexeme , v )
2022-05-20 15:47:04 +02:00
case typ . kind :
of UInt8 , Int8 :
2022-05-22 15:26:12 +02:00
result = self . chunk . writeConstant ( [ uint8 ( v ) ] )
2022-05-20 15:47:04 +02:00
of Int16 , UInt16 :
2022-05-22 15:26:12 +02:00
result = self . chunk . writeConstant ( v . toDouble ( ) )
2022-05-20 15:47:04 +02:00
of Int32 , UInt32 :
2022-05-22 15:26:12 +02:00
result = self . chunk . writeConstant ( v . toQuad ( ) )
2022-05-20 15:47:04 +02:00
of Int64 , UInt64 :
2022-05-22 15:26:12 +02:00
result = self . chunk . writeConstant ( v . toLong ( ) )
2022-05-20 15:47:04 +02:00
else :
discard
2022-04-04 12:29:23 +02:00
2022-05-07 10:48:01 +02:00
proc emitConstant ( self : Compiler , obj : Expression , kind : Type ) =
2022-05-29 15:54:01 +02:00
## Emits a constant instruction along
2022-04-04 12:29:23 +02:00
## with its operand
2022-05-04 14:27:15 +02:00
case self . inferType ( obj ) . kind :
2022-05-02 17:26:38 +02:00
of Int64 :
self . emitByte ( LoadInt64 )
2022-05-29 15:54:01 +02:00
of UInt64 :
self . emitByte ( LoadUInt64 )
of Int32 :
self . emitByte ( LoadInt32 )
2022-05-02 17:26:38 +02:00
else :
2022-05-18 13:32:32 +02:00
discard # TODO
2022-05-07 10:48:01 +02:00
self . emitBytes ( self . makeConstant ( obj , kind ) )
2022-04-04 12:29:23 +02:00
proc emitJump ( self : Compiler , opcode : OpCode ) : int =
## Emits a dummy jump offset to be patched later. Assumes
## the largest offset (emits 4 bytes, one for the given jump
2022-05-29 15:54:01 +02:00
## opcode, while the other 3 are for the jump offset, which
## is set to the maximum unsigned 24 bit integer). If the shorter
2022-04-04 12:29:23 +02:00
## 16 bit alternative is later found to be better suited, patchJump
2022-05-29 15:54:01 +02:00
## will fix this. Returns the absolute index into the chunk's
## bytecode array where the given placeholder instruction was written
2022-04-04 12:29:23 +02:00
self . emitByte ( opcode )
self . emitBytes ( ( 0xffffff ) . toTriple ( ) )
result = self . chunk . code . len ( ) - 4
proc patchJump ( self : Compiler , offset : int ) =
2022-05-18 13:32:32 +02:00
## Patches a previously emitted relative
2022-05-01 13:07:50 +02:00
## jump using emitJump. Since emitJump assumes
2022-04-04 12:29:23 +02:00
## a long jump, this also shrinks the jump
2022-05-29 15:54:01 +02:00
## offset and changes the bytecode instruction
## if possible (i.e. jump is in 16 bit range),
## but the converse is also true (i.e. it might
## change a regular jump into a long one)
2022-05-16 19:23:38 +02:00
var jump : int = self . chunk . code . len ( ) - offset
2022-04-04 12:29:23 +02:00
if jump > 16777215 :
2022-05-01 13:07:50 +02:00
self . error ( " cannot jump more than 16777216 bytecode instructions " )
2022-04-04 12:29:23 +02:00
if jump < uint16 . high ( ) . int :
case OpCode ( self . chunk . code [ offset ] ) :
of LongJumpForwards :
self . chunk . code [ offset ] = JumpForwards . uint8 ( )
2022-05-29 15:54:01 +02:00
# We do this because a relative jump
# does not take its argument into account
# because it is hardcoded in the bytecode
# itself
2022-05-24 22:26:45 +02:00
jump - = 4
2022-04-04 12:29:23 +02:00
of LongJumpBackwards :
self . chunk . code [ offset ] = JumpBackwards . uint8 ( )
2022-05-24 22:26:45 +02:00
jump - = 4
2022-04-04 12:29:23 +02:00
of LongJumpIfFalse :
self . chunk . code [ offset ] = JumpIfFalse . uint8 ( )
of LongJumpIfFalsePop :
self . chunk . code [ offset ] = JumpIfFalsePop . uint8 ( )
of LongJumpIfFalseOrPop :
self . chunk . code [ offset ] = JumpIfFalseOrPop . uint8 ( )
2022-05-24 22:26:45 +02:00
of JumpForwards , JumpBackwards :
jump - = 3
2022-04-04 12:29:23 +02:00
else :
discard
2022-05-29 15:54:01 +02:00
self . chunk . code . delete ( offset + 1 ) # Discards the first 8 bits of the jump offset (which are empty)
2022-04-26 13:04:40 +02:00
let offsetArray = ( jump - 1 ) . toDouble ( ) # -1 since we got rid of 1 byte!
2022-04-04 12:29:23 +02:00
self . chunk . code [ offset + 1 ] = offsetArray [ 0 ]
self . chunk . code [ offset + 2 ] = offsetArray [ 1 ]
else :
case OpCode ( self . chunk . code [ offset ] ) :
of JumpForwards :
self . chunk . code [ offset ] = LongJumpForwards . uint8 ( )
2022-05-24 22:26:45 +02:00
jump - = 3
2022-04-04 12:29:23 +02:00
of JumpBackwards :
self . chunk . code [ offset ] = LongJumpBackwards . uint8 ( )
2022-05-24 22:26:45 +02:00
jump - = 3
2022-04-04 12:29:23 +02:00
of JumpIfFalse :
self . chunk . code [ offset ] = LongJumpIfFalse . uint8 ( )
of JumpIfFalsePop :
self . chunk . code [ offset ] = LongJumpIfFalsePop . uint8 ( )
of JumpIfFalseOrPop :
self . chunk . code [ offset ] = LongJumpIfFalseOrPop . uint8 ( )
2022-05-24 22:26:45 +02:00
of LongJumpForwards , LongJumpBackwards :
jump - = 4
2022-04-04 12:29:23 +02:00
else :
discard
let offsetArray = jump . toTriple ( )
self . chunk . code [ offset + 1 ] = offsetArray [ 0 ]
self . chunk . code [ offset + 2 ] = offsetArray [ 1 ]
self . chunk . code [ offset + 3 ] = offsetArray [ 2 ]
2022-05-02 17:26:38 +02:00
proc resolve ( self : Compiler , name : IdentExpr ,
depth : int = self . scopeDepth ) : Name =
## Traverses self.names backwards and returns the
## first name object with the given name. Returns
## nil when the name can't be found. This function
## has no concept of scope depth, because getStackPos
## does that job. Note that private names declared in
## other modules will not be resolved!
for obj in reversed ( self . names ) :
if obj . name . token . lexeme = = name . token . lexeme :
if obj . isPrivate and obj . owner ! = self . currentModule :
2022-05-26 18:31:40 +02:00
continue # There may be a name in the current module that
2022-05-02 17:26:38 +02:00
# matches, so we skip this
return obj
return nil
2022-05-18 13:32:32 +02:00
proc getStackPos ( self : Compiler , name : IdentExpr ,
depth : int = self . scopeDepth ) : tuple [ closedOver : bool , pos : int ] =
2022-05-02 17:26:38 +02:00
## Iterates the internal list of declared names backwards and
## returns a tuple (closedOver, pos) that tells the caller whether the
## the name is to be emitted as a closure as well as its predicted
2022-05-18 13:32:32 +02:00
## stack/closure array position. Returns (false, -1) if the variable's
2022-05-02 17:26:38 +02:00
## location can not be determined at compile time (this is an error!).
## Note that private names declared in other modules will not be resolved!
var i : int = self . names . high ( )
for variable in reversed ( self . names ) :
if name . name . lexeme = = variable . name . name . lexeme :
if variable . isPrivate and variable . owner ! = self . currentModule :
continue
2022-05-26 18:31:40 +02:00
elif variable . depth = = depth or variable . depth = = 0 :
2022-05-02 17:26:38 +02:00
# variable.depth == 0 for globals!
return ( false , i )
elif variable . depth > 0 :
2022-05-26 18:31:40 +02:00
var j : int = self . closedOver . high ( )
for closure in reversed ( self . closedOver ) :
if closure . name . token . lexeme = = name . name . lexeme :
2022-05-02 17:26:38 +02:00
return ( true , j )
2022-05-26 18:31:40 +02:00
inc ( j )
2022-05-02 17:26:38 +02:00
dec ( i )
return ( false , - 1 )
2022-05-26 18:31:40 +02:00
proc detectClosureVariable ( self : Compiler , name : Name ,
2022-05-18 13:32:32 +02:00
depth : int = self . scopeDepth ) =
2022-05-02 17:26:38 +02:00
## Detects if the given name is used in a local scope deeper
## than the given one and modifies the code emitted for it
## to store it as a closure variable if it is. Does nothing if the name
## hasn't been declared yet or is unreachable (for example if it's
2022-05-04 14:01:38 +02:00
## declared as private in another module). This function must be called
2022-05-18 13:32:32 +02:00
## each time a name is referenced in order for closed-over variables
## to be emitted properly, otherwise the runtime may behave
2022-05-02 17:26:38 +02:00
## unpredictably or crash
2022-05-26 18:31:40 +02:00
if name = = nil :
2022-05-02 17:26:38 +02:00
return
2022-05-26 18:31:40 +02:00
if name . depth < depth :
2022-05-02 17:26:38 +02:00
# Ding! The given name is closed over: we need to
2022-05-26 18:31:40 +02:00
# change the NoOp instructions that self.declareName
# put in place for us into a StoreHeap. We don't need to change
2022-05-02 17:26:38 +02:00
# other pieces of code because self.identifier() already
# emits LoadHeap if it detects the variable is closed over,
# whether or not this function is called
2022-05-26 18:31:40 +02:00
self . closedOver . add ( name )
2022-05-27 14:01:57 +02:00
let idx = self . closedOver . high ( ) . toTriple ( )
2022-05-02 17:26:38 +02:00
if self . closedOver . len ( ) > = 16777216 :
2022-05-04 14:01:38 +02:00
self . error ( " too many consecutive closed-over variables (max is 16777216) " )
2022-05-26 18:31:40 +02:00
self . chunk . code [ name . codePos ] = StoreHeap . uint8
self . chunk . code [ name . codePos + 1 ] = idx [ 0 ]
self . chunk . code [ name . codePos + 2 ] = idx [ 1 ]
self . chunk . code [ name . codePos + 3 ] = idx [ 2 ]
2022-05-27 14:01:57 +02:00
name . isClosedOver = true
2022-05-02 17:26:38 +02:00
2022-05-22 13:02:48 +02:00
2022-05-07 10:48:01 +02:00
proc compareTypes ( self : Compiler , a , b : Type ) : bool =
## Compares two type objects
## for equality (works with nil!)
2022-05-29 15:54:01 +02:00
# The nil code here is for void functions (when
# we compare their return types)
2022-05-07 10:48:01 +02:00
if a = = nil :
return b = = nil
elif b = = nil :
2022-05-29 15:54:01 +02:00
return a = = nil
elif a . kind ! = b . kind :
# Next, we see the type discriminant:
# If they're different, then they can't
# be the same type!
2022-05-07 10:48:01 +02:00
return false
case a . kind :
2022-05-29 15:54:01 +02:00
# If all previous checks pass, it's time
# to go through each possible type peon
# supports and compare it
2022-05-07 10:48:01 +02:00
of Int8 , UInt8 , Int16 , UInt16 , Int32 ,
UInt32 , Int64 , UInt64 , Float32 , Float64 ,
Char , Byte , String , Nil , Nan , Bool , Inf :
2022-05-29 15:54:01 +02:00
# A value type's type is always equal to
# another one's
2022-05-07 10:48:01 +02:00
return true
2022-05-29 15:54:01 +02:00
of Reference , Pointer , Mutable :
# Here we already know that both
# a and b are of either of the three
# types in this branch, so we just need
# to compare their values
return self . compareTypes ( a . value , b . value )
2022-05-07 10:48:01 +02:00
of Function :
2022-05-29 15:54:01 +02:00
# Functions are a bit trickier
if a . args . len ( ) ! = b . args . len ( ) :
return false
elif not self . compareTypes ( a . returnType , b . returnType ) :
2022-05-29 23:01:36 +02:00
if a . returnType ! = nil and b . returnType ! = nil :
if a . returnType . kind ! = Any and b . returnType . kind ! = Any :
return false
return false
2022-05-29 15:54:01 +02:00
for ( argA , argB ) in zip ( a . args , b . args ) :
if not self . compareTypes ( argA . kind , argB . kind ) :
2022-05-07 10:48:01 +02:00
return false
2022-05-29 15:54:01 +02:00
return true
2022-05-07 10:48:01 +02:00
else :
discard
proc toIntrinsic ( name : string ) : Type =
2022-05-18 13:32:32 +02:00
## Converts a string to an intrinsic
2022-05-07 10:48:01 +02:00
## type if it is valid and returns nil
2022-05-18 13:32:32 +02:00
## otherwise
2022-05-07 10:48:01 +02:00
if name in [ " int " , " int64 " , " i64 " ] :
return Type ( kind : Int64 )
elif name in [ " uint64 " , " u64 " ] :
return Type ( kind : UInt64 )
elif name in [ " int32 " , " i32 " ] :
return Type ( kind : Int32 )
elif name in [ " uint32 " , " u32 " ] :
return Type ( kind : UInt32 )
elif name in [ " int16 " , " i16 " ] :
return Type ( kind : Int16 )
elif name in [ " uint16 " , " u16 " ] :
return Type ( kind : UInt16 )
elif name in [ " int8 " , " i8 " ] :
return Type ( kind : Int8 )
elif name in [ " uint8 " , " u8 " ] :
return Type ( kind : UInt8 )
elif name in [ " f64 " , " float " , " float64 " ] :
return Type ( kind : Float64 )
elif name in [ " f32 " , " float32 " ] :
return Type ( kind : Float32 )
elif name = = " byte " :
return Type ( kind : Byte )
elif name = = " char " :
return Type ( kind : Char )
elif name = = " nan " :
return Type ( kind : Nan )
elif name = = " nil " :
return Type ( kind : Nil )
elif name = = " inf " :
return Type ( kind : Inf )
elif name = = " bool " :
return Type ( kind : Bool )
2022-05-23 11:53:34 +02:00
elif name = = " type " :
return Type ( kind : Typedesc )
2022-05-07 10:48:01 +02:00
else :
return nil
2022-05-02 17:26:38 +02:00
2022-05-29 15:54:01 +02:00
proc toIntrinsic ( self : Compiler , typ : Expression ) : Type =
## Gets an expression's intrinsic type, if
## possible
if typ = = nil :
return nil
case typ . kind :
of trueExpr , falseExpr , intExpr , floatExpr :
return typ . token . lexeme . toIntrinsic ( )
of identExpr :
let inferred = self . inferType ( typ )
if inferred = = nil :
return typ . token . lexeme . toIntrinsic ( )
return inferred
else :
discard
2022-05-04 14:27:15 +02:00
proc inferType ( self : Compiler , node : LiteralExpr ) : Type =
2022-05-02 17:26:38 +02:00
## Infers the type of a given literal expression
2022-05-16 19:23:38 +02:00
if node = = nil :
return nil
2022-05-02 17:26:38 +02:00
case node . kind :
of intExpr , binExpr , octExpr , hexExpr :
let size = node . token . lexeme . split ( " ' " )
if len ( size ) notin 1 .. 2 :
self . error ( " invalid state: inferValueType -> invalid size specifier (This is an internal error and most likely a bug!) " )
if size . len ( ) = = 1 :
2022-05-29 15:54:01 +02:00
return Type ( kind : Int64 )
2022-05-02 17:26:38 +02:00
let typ = size [ 1 ] . toIntrinsic ( )
2022-05-07 10:48:01 +02:00
if not self . compareTypes ( typ , nil ) :
2022-05-02 17:26:38 +02:00
return typ
else :
self . error ( & " invalid type specifier ' {size[1]} ' for int " )
of floatExpr :
let size = node . token . lexeme . split ( " ' " )
if len ( size ) notin 1 .. 2 :
self . error ( " invalid state: inferValueType -> invalid size specifier (This is an internal error and most likely a bug!) " )
if size . len ( ) = = 1 or size [ 1 ] = = " f64 " :
2022-05-29 15:54:01 +02:00
return Type ( kind : Float64 )
2022-05-02 17:26:38 +02:00
let typ = size [ 1 ] . toIntrinsic ( )
2022-05-07 10:48:01 +02:00
if not self . compareTypes ( typ , nil ) :
2022-05-02 17:26:38 +02:00
return typ
else :
self . error ( & " invalid type specifier ' {size[1]} ' for float " )
of nilExpr :
2022-05-29 15:54:01 +02:00
return Type ( kind : Nil )
2022-05-02 17:26:38 +02:00
of trueExpr :
2022-05-29 15:54:01 +02:00
return Type ( kind : Bool )
2022-05-02 17:26:38 +02:00
of falseExpr :
2022-05-29 15:54:01 +02:00
return Type ( kind : Bool )
2022-05-02 17:26:38 +02:00
of nanExpr :
2022-05-29 15:54:01 +02:00
return Type ( kind : TypeKind . Nan )
2022-05-02 17:26:38 +02:00
of infExpr :
2022-05-29 15:54:01 +02:00
return Type ( kind : TypeKind . Inf )
2022-05-02 17:26:38 +02:00
else :
2022-05-18 13:32:32 +02:00
discard # TODO
2022-05-02 17:26:38 +02:00
2022-05-04 14:27:15 +02:00
proc inferType ( self : Compiler , node : Expression ) : Type =
2022-05-02 17:26:38 +02:00
## Infers the type of a given expression and
## returns it
2022-05-16 19:23:38 +02:00
if node = = nil :
return nil
2022-05-02 17:26:38 +02:00
case node . kind :
of identExpr :
2022-05-07 10:48:01 +02:00
let node = IdentExpr ( node )
let name = self . resolve ( node )
2022-05-04 14:27:15 +02:00
if name ! = nil :
return name . valueType
2022-05-07 10:48:01 +02:00
else :
2022-05-25 14:17:58 +02:00
result = node . name . lexeme . toIntrinsic ( )
2022-05-02 17:26:38 +02:00
of unaryExpr :
2022-05-04 14:27:15 +02:00
return self . inferType ( UnaryExpr ( node ) . a )
2022-05-02 17:26:38 +02:00
of binaryExpr :
2022-05-04 14:27:15 +02:00
let node = BinaryExpr ( node )
var a = self . inferType ( node . a )
var b = self . inferType ( node . b )
2022-05-07 10:48:01 +02:00
if not self . compareTypes ( a , b ) :
2022-05-02 17:26:38 +02:00
return nil
return a
2022-05-18 13:32:32 +02:00
of { intExpr , hexExpr , binExpr , octExpr ,
strExpr , falseExpr , trueExpr , infExpr ,
2022-05-04 14:01:38 +02:00
nanExpr , floatExpr , nilExpr
2022-05-02 17:26:38 +02:00
} :
2022-05-04 14:27:15 +02:00
return self . inferType ( LiteralExpr ( node ) )
2022-05-27 14:01:57 +02:00
of lambdaExpr :
var node = LambdaExpr ( node )
2022-05-29 15:54:01 +02:00
result = Type ( kind : Function , returnType : nil , args : @ [ ] , isLambda : true )
2022-05-27 14:01:57 +02:00
if node . returnType ! = nil :
result . returnType = self . inferType ( node . returnType )
for argument in node . arguments :
2022-05-29 15:54:01 +02:00
result . args . add ( ( argument . name . token . lexeme , self . inferType ( argument . valueType ) ) )
2022-05-02 17:26:38 +02:00
else :
2022-05-18 13:32:32 +02:00
discard # Unreachable
2022-05-02 17:26:38 +02:00
2022-05-07 10:48:01 +02:00
proc inferType ( self : Compiler , node : Declaration ) : Type =
## Infers the type of a given declaration
## and returns it
2022-05-16 19:23:38 +02:00
if node = = nil :
return nil
2022-05-07 10:48:01 +02:00
case node . kind :
of funDecl :
var node = FunDecl ( node )
let resolved = self . resolve ( node . name )
if resolved ! = nil :
return resolved . valueType
of NodeKind . varDecl :
var node = VarDecl ( node )
let resolved = self . resolve ( node . name )
if resolved ! = nil :
return resolved . valueType
else :
return self . inferType ( node . value )
2022-05-02 17:26:38 +02:00
else :
2022-05-18 13:32:32 +02:00
return # Unreachable
2022-05-04 14:01:38 +02:00
2022-05-29 15:54:01 +02:00
proc typeToStr ( self : Compiler , typ : Type ) : string =
## Returns the string representation of a
## type object
case typ . kind :
of Int8 , UInt8 , Int16 , UInt16 , Int32 ,
UInt32 , Int64 , UInt64 , Float32 , Float64 ,
Char , Byte , String , Nil , TypeKind . Nan , Bool ,
TypeKind . Inf :
return ( $ typ . kind ) . toLowerAscii ( )
of Pointer :
return & " ptr {self.typeToStr(typ.value)} "
of Reference :
return & " ref {self.typeToStr(typ.value)} "
of Mutable :
return & " var {self.typeToStr(typ.value)} "
of Function :
result = " fn ( "
for i , ( argName , argType ) in typ . args :
result & = & " {argName}: {self.typeToStr(argType)} "
if i < typ . args . len ( ) - 1 :
result & = " , "
result & = " ) "
if typ . returnType ! = nil :
result & = & " : {self.typeToStr(typ.returnType)} "
else :
discard
2022-04-04 12:29:23 +02:00
## End of utility functions
2022-05-07 10:48:01 +02:00
2022-04-04 12:29:23 +02:00
proc literal ( self : Compiler , node : ASTNode ) =
## Emits instructions for literals such
## as singletons, strings, numbers and
## collections
case node . kind :
of trueExpr :
2022-05-07 10:48:01 +02:00
self . emitByte ( LoadTrue )
2022-04-04 12:29:23 +02:00
of falseExpr :
2022-05-07 10:48:01 +02:00
self . emitByte ( LoadFalse )
2022-04-04 12:29:23 +02:00
of nilExpr :
2022-05-07 10:48:01 +02:00
self . emitByte ( LoadNil )
2022-04-04 12:29:23 +02:00
of infExpr :
2022-05-07 10:48:01 +02:00
self . emitByte ( LoadInf )
2022-04-04 12:29:23 +02:00
of nanExpr :
2022-05-07 10:48:01 +02:00
self . emitByte ( LoadNan )
2022-04-04 12:29:23 +02:00
of strExpr :
2022-05-07 10:48:01 +02:00
self . emitConstant ( LiteralExpr ( node ) , Type ( kind : String ) )
# TODO: Take size specifier into account!
2022-04-04 12:29:23 +02:00
of intExpr :
var x : int
var y = IntExpr ( node )
try :
2022-04-26 09:29:59 +02:00
discard parseInt ( y . literal . lexeme , x )
2022-04-04 12:29:23 +02:00
except ValueError :
self . error ( " integer value out of range " )
2022-05-07 10:48:01 +02:00
self . emitConstant ( y , Type ( kind : Int64 ) )
2022-04-04 12:29:23 +02:00
of hexExpr :
var x : int
var y = HexExpr ( node )
try :
2022-04-26 09:29:59 +02:00
discard parseHex ( y . literal . lexeme , x )
2022-04-04 12:29:23 +02:00
except ValueError :
self . error ( " integer value out of range " )
2022-05-18 13:32:32 +02:00
let node = newIntExpr ( Token ( lexeme : $ x , line : y . token . line ,
pos : ( start : y . token . pos . start ,
2022-05-07 10:48:01 +02:00
stop : y . token . pos . start + len ( $ x ) )
2022-05-18 13:32:32 +02:00
)
)
2022-05-07 10:48:01 +02:00
self . emitConstant ( node , Type ( kind : Int64 ) )
2022-04-04 12:29:23 +02:00
of binExpr :
var x : int
var y = BinExpr ( node )
try :
2022-04-26 09:29:59 +02:00
discard parseBin ( y . literal . lexeme , x )
2022-04-04 12:29:23 +02:00
except ValueError :
self . error ( " integer value out of range " )
2022-05-18 13:32:32 +02:00
let node = newIntExpr ( Token ( lexeme : $ x , line : y . token . line ,
pos : ( start : y . token . pos . start ,
2022-05-07 10:48:01 +02:00
stop : y . token . pos . start + len ( $ x ) )
2022-05-18 13:32:32 +02:00
)
)
2022-05-07 10:48:01 +02:00
self . emitConstant ( node , Type ( kind : Int64 ) )
2022-04-04 12:29:23 +02:00
of octExpr :
var x : int
var y = OctExpr ( node )
try :
2022-04-26 09:29:59 +02:00
discard parseOct ( y . literal . lexeme , x )
2022-04-04 12:29:23 +02:00
except ValueError :
self . error ( " integer value out of range " )
2022-05-18 13:32:32 +02:00
let node = newIntExpr ( Token ( lexeme : $ x , line : y . token . line ,
pos : ( start : y . token . pos . start ,
2022-05-07 10:48:01 +02:00
stop : y . token . pos . start + len ( $ x ) )
2022-05-18 13:32:32 +02:00
)
)
2022-05-07 10:48:01 +02:00
self . emitConstant ( node , Type ( kind : Int64 ) )
2022-04-04 12:29:23 +02:00
of floatExpr :
var x : float
var y = FloatExpr ( node )
try :
2022-04-26 09:29:59 +02:00
discard parseFloat ( y . literal . lexeme , x )
2022-04-04 12:29:23 +02:00
except ValueError :
self . error ( " floating point value out of range " )
2022-05-07 10:48:01 +02:00
self . emitConstant ( y , Type ( kind : Float64 ) )
2022-04-04 12:29:23 +02:00
of awaitExpr :
var y = AwaitExpr ( node )
2022-04-07 13:02:23 +02:00
self . expression ( y . expression )
2022-04-04 12:29:23 +02:00
self . emitByte ( OpCode . Await )
else :
2022-05-01 13:07:50 +02:00
self . error ( & " invalid AST node of kind {node.kind} at literal(): {node} (This is an internal error and most likely a bug!) " )
2022-04-04 12:29:23 +02:00
2022-05-24 09:55:08 +02:00
proc matchImpl ( self : Compiler , name : string , kind : Type ) : Name =
## Tries to find a matching function implementation
## compatible with the given type and returns its
## name object
let impl = self . findByType ( name , kind )
2022-05-22 13:02:48 +02:00
if impl . len ( ) = = 0 :
2022-05-24 09:55:08 +02:00
var msg = & " cannot find a suitable implementation for ' {name} ' "
let names = self . findByName ( name )
if names . len ( ) > 0 :
2022-05-24 10:23:34 +02:00
msg & = & " , found {len(names)} candidate "
if names . len ( ) > 1 :
msg & = " s "
msg & = " : "
2022-05-24 09:55:08 +02:00
for name in names :
2022-05-24 10:23:34 +02:00
msg & = & " \n - ' {name.name.token.lexeme} ' of type ' {self.typeToStr(name.valueType)} ' "
if name . valueType . kind ! = Function :
msg & = " , not a callable "
elif kind . args . len ( ) ! = name . valueType . args . len ( ) :
2022-05-24 22:26:45 +02:00
msg & = & " , wrong number of arguments ({name.valueType.args.len()} expected, got {kind.args.len()}) "
2022-05-24 10:23:34 +02:00
else :
for i , arg in kind . args :
2022-05-29 15:54:01 +02:00
if not self . compareTypes ( arg . kind , name . valueType . args [ i ] . kind ) :
msg & = & " , first mismatch at position {i + 1}: expected argument of type ' {self.typeToStr(name.valueType.args[i].kind)} ' , got ' {self.typeToStr(arg.kind)} ' instead "
2022-05-24 09:55:08 +02:00
self . error ( msg )
elif impl . len ( ) > 1 :
var msg = & " multiple matching implementations of ' {name} ' found: \n "
2022-05-22 17:23:52 +02:00
for fn in reversed ( impl ) :
2022-05-29 15:54:01 +02:00
msg & = & " - ' {fn.name} ' at line {fn.line} of type {self.typeToStr(fn.valueType)} \n "
2022-05-22 17:23:52 +02:00
self . error ( msg )
2022-05-24 10:23:34 +02:00
return impl [ 0 ]
2022-05-24 09:55:08 +02:00
proc callUnaryOp ( self : Compiler , fn : Name , op : UnaryExpr ) =
## Emits the code to call a unary operator
# Pushes the return address
self . emitByte ( LoadUInt32 )
# We patch it later!
let idx = self . chunk . consts . len ( )
self . emitBytes ( self . chunk . writeConstant ( ( 0xffffffff 'u32 ) . toQuad ( ) ) )
self . expression ( op . a ) # Pushes the arguments onto the stack
self . emitByte ( Call ) # Creates a stack frame
self . emitBytes ( fn . codePos . toTriple ( ) )
self . emitBytes ( 1 . toTriple ( ) )
self . patchReturnAddress ( idx )
proc callBinaryOp ( self : Compiler , fn : Name , op : BinaryExpr ) =
## Emits the code to call a binary operator
# Pushes the return address
self . emitByte ( LoadUInt32 )
# We patch it later!
let idx = self . chunk . consts . len ( )
self . emitBytes ( self . chunk . writeConstant ( ( 0xffffffff 'u32 ) . toQuad ( ) ) )
self . expression ( op . a ) # Pushes the arguments onto the stack
self . expression ( op . b )
self . emitByte ( Call ) # Creates a stack frame
self . emitBytes ( fn . codePos . toTriple ( ) )
2022-05-27 14:01:57 +02:00
self . emitBytes ( 2 . toTriple ( ) )
2022-05-24 09:55:08 +02:00
self . patchReturnAddress ( idx )
proc unary ( self : Compiler , node : UnaryExpr ) =
## Compiles unary expressions such as decimal
## and bitwise negation
let valueType = self . inferType ( node . a )
2022-05-29 15:54:01 +02:00
let funct = self . matchImpl ( node . token . lexeme , Type ( kind : Function , returnType : Type ( kind : Any ) , args : @ [ ( " " , valueType ) ] ) )
2022-05-24 09:55:08 +02:00
self . callUnaryOp ( funct , node )
2022-04-04 12:29:23 +02:00
proc binary ( self : Compiler , node : BinaryExpr ) =
## Compiles all binary expressions
2022-05-24 09:55:08 +02:00
let typeOfA = self . inferType ( node . a )
let typeOfB = self . inferType ( node . b )
2022-05-29 15:54:01 +02:00
let funct = self . matchImpl ( node . token . lexeme , Type ( kind : Function , returnType : Type ( kind : Any ) , args : @ [ ( " " , typeOfA ) , ( " " , typeOfB ) ] ) )
2022-05-24 09:55:08 +02:00
self . callBinaryOp ( funct , node )
# TODO: Get rid of old code
#[
2022-04-04 12:29:23 +02:00
case node . operator . kind :
2022-05-07 10:48:01 +02:00
of NoMatch :
2022-05-01 13:07:50 +02:00
# a and b
2022-04-04 12:29:23 +02:00
self . expression ( node . a )
var jump : int
if self . enableOptimizations :
jump = self . emitJump ( JumpIfFalseOrPop )
else :
jump = self . emitJump ( JumpIfFalse )
self . emitByte ( Pop )
self . expression ( node . b )
self . patchJump ( jump )
2022-05-07 10:48:01 +02:00
of EndOfFile :
2022-05-01 13:07:50 +02:00
# a or b
2022-04-04 12:29:23 +02:00
self . expression ( node . a )
let jump = self . emitJump ( JumpIfTrue )
self . expression ( node . b )
self . patchJump ( jump )
else :
2022-05-01 13:07:50 +02:00
self . error ( & " invalid AST node of kind {node.kind} at binary(): {node} (This is an internal error and most likely a bug!) " )
2022-05-24 09:55:08 +02:00
] #
2022-04-04 12:29:23 +02:00
2022-05-02 17:26:38 +02:00
proc declareName ( self : Compiler , node : Declaration ) =
2022-05-24 09:55:08 +02:00
## Statically declares a name into the current scope.
## "Declaring" a name only means updating our internal
## list of identifiers so that further calls to resolve()
## correctly return them. There is no code to actually
## declare a variable at runtime: the value is already
## there on the stack
2022-04-04 12:29:23 +02:00
case node . kind :
2022-04-21 15:25:29 +02:00
of NodeKind . varDecl :
2022-04-04 12:29:23 +02:00
var node = VarDecl ( node )
2022-04-26 09:29:59 +02:00
# Creates a new Name entry so that self.identifier emits the proper stack offset
2022-04-21 15:25:29 +02:00
if self . names . high ( ) > 16777215 :
# If someone ever hits this limit in real-world scenarios, I swear I'll
# slap myself 100 times with a sign saying "I'm dumb". Mark my words
2022-04-26 16:22:23 +02:00
self . error ( " cannot declare more than 16777216 variables at a time " )
2022-05-23 10:49:38 +02:00
for name in self . findByName ( node . name . token . lexeme ) :
2022-05-29 15:54:01 +02:00
if name . depth = = self . scopeDepth and name . valueType . kind notin { Function , CustomType } :
# Trying to redeclare a variable in the same module is an error!
self . error ( & " attempt to redeclare ' {node.name.token.lexeme} ' , which was previously defined in ' {name.owner} ' at line {name.line} " )
2022-05-18 13:32:32 +02:00
self . names . add ( Name ( depth : self . scopeDepth ,
2022-04-27 16:03:48 +02:00
name : node . name ,
2022-04-26 09:29:59 +02:00
isPrivate : node . isPrivate ,
owner : self . currentModule ,
isConst : node . isConst ,
2022-05-29 15:54:01 +02:00
valueType : Type ( kind : self . inferType ( node . value ) . kind ) ,
2022-04-29 23:04:53 +02:00
codePos : self . chunk . code . len ( ) ,
2022-05-27 14:01:57 +02:00
isLet : node . isLet ,
2022-05-29 15:54:01 +02:00
isClosedOver : false ,
line : node . token . line ) )
2022-05-27 14:01:57 +02:00
# We emit 4 No-Ops because they may become a
# StoreHeap instruction. If not, they'll be
# removed before the compiler is finished
2022-05-29 15:54:01 +02:00
# TODO: This may break CFI offsets
2022-05-26 18:31:40 +02:00
self . emitBytes ( [ NoOp , NoOp , NoOp , NoOp ] )
2022-05-01 13:07:50 +02:00
of NodeKind . funDecl :
2022-04-04 12:29:23 +02:00
var node = FunDecl ( node )
2022-04-27 16:03:48 +02:00
self . names . add ( Name ( depth : self . scopeDepth ,
isPrivate : node . isPrivate ,
isConst : false ,
owner : self . currentModule ,
2022-05-29 15:54:01 +02:00
valueType : Type ( kind : Function ,
2022-05-29 23:01:36 +02:00
name : node . name . token . lexeme ,
2022-05-18 13:32:32 +02:00
returnType : self . inferType (
2022-05-22 13:02:48 +02:00
node . returnType ) ,
args : @ [ ] ) ,
2022-05-22 17:23:52 +02:00
codePos : self . chunk . code . high ( ) ,
2022-04-29 23:04:53 +02:00
name : node . name ,
2022-05-27 14:01:57 +02:00
isLet : false ,
2022-05-29 15:54:01 +02:00
isClosedOver : false ,
line : node . token . line ) )
2022-05-22 13:02:48 +02:00
let fn = self . names [ ^ 1 ]
2022-05-29 15:54:01 +02:00
var name : Name
2022-04-04 12:29:23 +02:00
for argument in node . arguments :
if self . names . high ( ) > 16777215 :
2022-04-26 16:22:23 +02:00
self . error ( " cannot declare more than 16777216 variables at a time " )
2022-05-23 10:49:38 +02:00
# wait, no LoadVar?? Yes! That's because when calling functions,
# arguments will already be on the stack so there's no need to
# load them here
2022-05-29 15:54:01 +02:00
name = Name ( depth : self . scopeDepth + 1 ,
isPrivate : true ,
owner : self . currentModule ,
isConst : false ,
name : argument . name ,
valueType : nil ,
2022-05-29 17:04:19 +02:00
codePos : 0 ,
2022-05-29 15:54:01 +02:00
isLet : false ,
isClosedOver : false )
self . names . add ( name )
name . valueType = self . inferType ( argument . valueType )
if argument . mutable :
name . valueType = Type ( kind : Mutable , value : name . valueType )
elif argument . isRef :
name . valueType = Type ( kind : Reference , value : name . valueType )
elif argument . isPtr :
name . valueType = Type ( kind : Pointer , value : name . valueType )
2022-05-23 11:53:34 +02:00
# We check if the argument's type is a generic
2022-05-29 15:54:01 +02:00
if name . valueType = = nil and argument . valueType . kind = = identExpr :
2022-05-23 11:53:34 +02:00
for gen in node . generics :
if gen . name = = IdentExpr ( argument . valueType ) :
2022-05-29 15:54:01 +02:00
name . valueType = Type ( kind : Generic )
2022-05-23 11:53:34 +02:00
break
# If it's still nil, it's an error!
2022-05-29 15:54:01 +02:00
if name . valueType = = nil :
self . error ( & " cannot determine the type of argument ' {argument.name.token.lexeme} ' " )
fn . valueType . args . add ( ( argument . name . token . lexeme , name . valueType ) )
2022-04-04 12:29:23 +02:00
else :
2022-05-27 14:01:57 +02:00
discard # TODO: Types, enums
2022-05-18 13:32:32 +02:00
2022-04-27 16:03:48 +02:00
2022-04-04 12:29:23 +02:00
proc identifier ( self : Compiler , node : IdentExpr ) =
## Compiles access to identifiers
2022-04-27 16:03:48 +02:00
let s = self . resolve ( node )
if s = = nil :
2022-04-21 15:25:29 +02:00
self . error ( & " reference to undeclared name ' {node.token.lexeme} ' " )
2022-04-27 16:03:48 +02:00
elif s . isConst :
2022-05-26 18:31:40 +02:00
# Constants are always emitted as Load* instructions
# no matter the scope depth
2022-05-07 10:48:01 +02:00
self . emitConstant ( node , self . inferType ( node ) )
2022-04-27 16:03:48 +02:00
else :
2022-05-26 18:31:40 +02:00
self . detectClosureVariable ( s )
2022-04-27 16:03:48 +02:00
let t = self . getStackPos ( node )
2022-05-25 14:38:34 +02:00
var index = t . pos
2022-05-18 13:32:32 +02:00
# We don't check if index is -1 because if it
2022-04-27 16:03:48 +02:00
# were, self.resolve() would have returned nil
if not t . closedOver :
# Static name resolution, loads value at index in the stack. Very fast. Much wow.
self . emitByte ( LoadVar )
2022-05-24 22:26:45 +02:00
self . emitBytes ( ( index - self . frames [ self . scopeDepth ] ) . toTriple ( ) )
2022-04-27 16:03:48 +02:00
else :
# Heap-allocated closure variable. Stored in a separate "closure array" in the VM that does not have stack semantics.
2022-05-27 14:01:57 +02:00
# This makes closures work as expected and is not much slower than indexing our stack (since they're both
2022-04-27 16:03:48 +02:00
# dynamic arrays at runtime anyway)
self . emitByte ( LoadHeap )
self . emitBytes ( self . closedOver . high ( ) . toTriple ( ) )
2022-04-04 12:29:23 +02:00
2022-05-07 10:48:01 +02:00
proc findByName ( self : Compiler , name : string ) : seq [ Name ] =
## Looks for objects that have been already declared
2022-05-20 15:47:04 +02:00
## with the given name. Returns all objects that apply
2022-05-07 10:48:01 +02:00
for obj in reversed ( self . names ) :
if obj . name . token . lexeme = = name :
result . add ( obj )
proc findByType ( self : Compiler , name : string , kind : Type ) : seq [ Name ] =
## Looks for objects that have already been declared
## with the given name and type
for obj in self . findByName ( name ) :
if self . compareTypes ( obj . valueType , kind ) :
result . add ( obj )
2022-04-04 12:29:23 +02:00
proc assignment ( self : Compiler , node : ASTNode ) =
## Compiles assignment expressions
case node . kind :
of assignExpr :
2022-05-04 14:27:15 +02:00
let node = AssignExpr ( node )
2022-05-01 13:07:50 +02:00
let name = IdentExpr ( node . name )
2022-04-27 16:03:48 +02:00
let r = self . resolve ( name )
2022-04-29 23:04:53 +02:00
if r = = nil :
2022-05-04 14:27:15 +02:00
self . error ( & " assignment to undeclared name ' {name.token.lexeme} ' " )
2022-04-29 23:04:53 +02:00
elif r . isConst :
2022-05-07 10:48:01 +02:00
self . error ( & " cannot assign to ' {name.token.lexeme} ' (constant) " )
2022-04-29 23:04:53 +02:00
elif r . isLet :
2022-05-04 14:27:15 +02:00
self . error ( & " cannot reassign ' {name.token.lexeme} ' " )
2022-04-04 12:29:23 +02:00
self . expression ( node . value )
2022-04-27 16:03:48 +02:00
let t = self . getStackPos ( name )
2022-04-26 09:29:59 +02:00
let index = t . pos
2022-04-04 12:29:23 +02:00
if index ! = - 1 :
2022-04-26 09:29:59 +02:00
if not t . closedOver :
self . emitByte ( StoreVar )
else :
self . emitByte ( StoreHeap )
2022-04-04 12:29:23 +02:00
self . emitBytes ( index . toTriple ( ) )
else :
2022-04-21 15:25:29 +02:00
self . error ( & " reference to undeclared name ' {node.token.lexeme} ' " )
2022-04-04 12:29:23 +02:00
of setItemExpr :
2022-05-07 10:48:01 +02:00
let node = SetItemExpr ( node )
let typ = self . inferType ( node )
if typ = = nil :
self . error ( & " cannot determine the type of ' {node.name.token.lexeme} ' " )
2022-04-04 12:29:23 +02:00
# TODO
else :
self . error ( & " invalid AST node of kind {node.kind} at assignment(): {node} (This is an internal error and most likely a bug) " )
proc beginScope ( self : Compiler ) =
## Begins a new local scope by incrementing the current
## scope's depth
inc ( self . scopeDepth )
2022-05-24 22:26:45 +02:00
2022-04-04 12:29:23 +02:00
proc endScope ( self : Compiler ) =
## Ends the current local scope
2022-05-29 17:04:19 +02:00
if self . scopeDepth < 0 :
self . error ( " cannot call endScope with scopeDepth < 0 (This is an internal error and most likely a bug) " )
2022-05-20 15:47:04 +02:00
dec ( self . scopeDepth )
2022-04-04 12:29:23 +02:00
var popped : int = 0
2022-05-26 18:31:40 +02:00
var name : Name
2022-05-29 17:04:19 +02:00
var indeces : seq [ int ] = @ [ ]
2022-05-20 15:47:04 +02:00
for i , ident in reversed ( self . names ) :
2022-05-29 17:04:19 +02:00
if ident . depth > self . scopeDepth and ident . valueType . kind ! = TypeKind . Function :
2022-04-04 12:29:23 +02:00
inc ( popped )
2022-05-26 18:31:40 +02:00
name = self . names [ self . names . high ( ) - i ]
if name . valueType . kind ! = Function and OpCode ( self . chunk . code [ name . codePos ] ) = = NoOp :
2022-05-29 17:04:19 +02:00
for _ in countup ( 0 , 3 ) :
# Since by deleting it the size of the
# sequence decreases, we don't need to
# increase the index
self . chunk . code . delete ( name . codePos )
indeces . add ( self . names . high ( ) - i )
2022-04-04 12:29:23 +02:00
if not self . enableOptimizations :
# All variables with a scope depth larger than the current one
# are now out of scope. Begone, you're now homeless!
self . emitByte ( Pop )
if self . enableOptimizations and popped > 1 :
# If we're popping less than 65535 variables, then
# we can emit a PopN instruction. This is true for
# 99.99999% of the use cases of the language (who the
# hell is going to use 65 THOUSAND local variables?), but
2022-05-04 14:27:15 +02:00
# if you'll ever use more then Peon will emit a PopN instruction
2022-04-04 12:29:23 +02:00
# for the first 65 thousand and change local variables and then
# emit another batch of plain ol' Pop instructions for the rest
if popped < = uint16 . high ( ) . int ( ) :
self . emitByte ( PopN )
self . emitBytes ( popped . toDouble ( ) )
else :
self . emitByte ( PopN )
self . emitBytes ( uint16 . high ( ) . int . toDouble ( ) )
for i in countdown ( self . names . high ( ) , popped - uint16 . high ( ) . int ( ) ) :
if self . names [ i ] . depth > self . scopeDepth :
self . emitByte ( Pop )
elif popped = = 1 :
# We only emit PopN if we're popping more than one value
self . emitByte ( Pop )
2022-05-29 17:04:19 +02:00
for index in indeces :
self . names . delete ( index )
2022-04-04 12:29:23 +02:00
proc blockStmt ( self : Compiler , node : BlockStmt ) =
## Compiles block statements, which create a new
## local scope.
self . beginScope ( )
for decl in node . code :
self . declaration ( decl )
self . endScope ( )
proc ifStmt ( self : Compiler , node : IfStmt ) =
## Compiles if/else statements for conditional
## execution of code
self . expression ( node . condition )
var jumpCode : OpCode
if self . enableOptimizations :
jumpCode = JumpIfFalsePop
else :
jumpCode = JumpIfFalse
let jump = self . emitJump ( jumpCode )
if not self . enableOptimizations :
self . emitByte ( Pop )
self . statement ( node . thenBranch )
self . patchJump ( jump )
if node . elseBranch ! = nil :
let jump = self . emitJump ( JumpForwards )
self . statement ( node . elseBranch )
self . patchJump ( jump )
proc emitLoop ( self : Compiler , begin : int ) =
## Emits a JumpBackwards instruction with the correct
## jump offset
var offset : int
case OpCode ( self . chunk . code [ begin + 1 ] ) : # The jump instruction
of LongJumpForwards , LongJumpBackwards , LongJumpIfFalse ,
LongJumpIfFalsePop , LongJumpIfTrue :
offset = self . chunk . code . len ( ) - begin + 4
else :
offset = self . chunk . code . len ( ) - begin
if offset > uint16 . high ( ) . int :
if offset > 16777215 :
self . error ( " cannot jump more than 16777215 bytecode instructions " )
self . emitByte ( LongJumpBackwards )
self . emitBytes ( offset . toTriple ( ) )
else :
self . emitByte ( JumpBackwards )
self . emitBytes ( offset . toDouble ( ) )
proc whileStmt ( self : Compiler , node : WhileStmt ) =
2022-05-07 10:48:01 +02:00
## Compiles C-style while loops and
## desugared C-style for loops
2022-04-04 12:29:23 +02:00
let start = self . chunk . code . len ( )
self . expression ( node . condition )
2022-05-07 10:48:01 +02:00
var jump : int
if self . enableOptimizations :
jump = self . emitJump ( JumpIfFalsePop )
else :
jump = self . emitJump ( JumpIfFalse )
self . emitByte ( Pop )
2022-04-04 12:29:23 +02:00
self . statement ( node . body )
self . patchJump ( jump )
self . emitLoop ( start )
2022-05-04 14:27:15 +02:00
proc expression ( self : Compiler , node : Expression ) =
2022-04-04 12:29:23 +02:00
## Compiles all expressions
2022-05-04 14:27:15 +02:00
if self . inferType ( node ) = = nil :
2022-05-02 12:38:43 +02:00
if node . kind ! = identExpr :
# So we can raise a more appropriate
# error in self.identifier()
self . error ( " expression has no type " )
2022-04-04 12:29:23 +02:00
case node . kind :
2022-05-16 19:40:13 +02:00
of callExpr :
2022-05-18 13:32:32 +02:00
discard # TODO
2022-04-04 12:29:23 +02:00
of getItemExpr :
2022-05-18 13:32:32 +02:00
discard # TODO
2022-04-04 12:29:23 +02:00
# Note that for setItem and assign we don't convert
# the node to its true type because that type information
# would be lost in the call anyway. The differentiation
2022-05-07 10:48:01 +02:00
# happens in self.assignment()
2022-04-04 12:29:23 +02:00
of setItemExpr , assignExpr :
self . assignment ( node )
of identExpr :
self . identifier ( IdentExpr ( node ) )
of unaryExpr :
# Unary expressions such as ~5 and -3
self . unary ( UnaryExpr ( node ) )
of groupingExpr :
# Grouping expressions like (2 + 1)
self . expression ( GroupingExpr ( node ) . expression )
of binaryExpr :
# Binary expressions such as 2 ^ 5 and 0.66 * 3.14
self . binary ( BinaryExpr ( node ) )
of intExpr , hexExpr , binExpr , octExpr , strExpr , falseExpr , trueExpr ,
2022-05-04 14:01:38 +02:00
infExpr , nanExpr , floatExpr , nilExpr :
2022-05-07 10:48:01 +02:00
# Since all of these AST nodes share the
# same overall structure and the kind
# field is enough to tell one from the
2022-05-18 13:32:32 +02:00
# other, why bother with specialized
2022-05-07 10:48:01 +02:00
# cases when one is enough?
2022-04-04 12:29:23 +02:00
self . literal ( node )
else :
self . error ( & " invalid AST node of kind {node.kind} at expression(): {node} (This is an internal error and most likely a bug) " )
proc awaitStmt ( self : Compiler , node : AwaitStmt ) =
## Compiles await statements. An await statement
## is like an await expression, but parsed in the
## context of statements for usage outside expressions,
## meaning it can be used standalone. It's basically the
## same as an await expression followed by a semicolon.
## Await expressions are the only native construct to
## run coroutines from within an already asynchronous
2022-04-26 09:29:59 +02:00
## context (which should be orchestrated by an event loop).
2022-04-04 12:29:23 +02:00
## They block in the caller until the callee returns
2022-04-07 13:02:23 +02:00
self . expression ( node . expression )
2022-04-04 12:29:23 +02:00
self . emitByte ( OpCode . Await )
proc deferStmt ( self : Compiler , node : DeferStmt ) =
## Compiles defer statements. A defer statement
2022-04-26 09:29:59 +02:00
## is executed right before its containing function
## exits (either because of a return or an exception)
2022-04-04 12:29:23 +02:00
let current = self . chunk . code . len
2022-04-07 13:02:23 +02:00
self . expression ( node . expression )
2022-04-04 12:29:23 +02:00
for i in countup ( current , self . chunk . code . high ( ) ) :
self . deferred . add ( self . chunk . code [ i ] )
self . chunk . code . del ( i )
proc returnStmt ( self : Compiler , node : ReturnStmt ) =
## Compiles return statements. An empty return
## implicitly returns nil
2022-05-04 14:27:15 +02:00
let returnType = self . inferType ( node . value )
let typ = self . inferType ( self . currentFunction )
2022-05-16 19:23:38 +02:00
## Having the return type
2022-05-23 11:53:34 +02:00
if returnType = = nil and typ . returnType ! = nil :
2022-05-16 19:23:38 +02:00
self . error ( & " expected return value of type ' {self.typeToStr(typ.returnType)} ' , but expression has no type " )
2022-05-23 11:53:34 +02:00
elif typ . returnType = = nil and returnType ! = nil :
self . error ( " empty return statement is not allowed in non-void functions " )
2022-05-07 10:48:01 +02:00
elif not self . compareTypes ( returnType , typ . returnType ) :
self . error ( & " expected return value of type ' {self.typeToStr(typ.returnType)} ' , got ' {self.typeToStr(returnType)} ' instead " )
2022-05-16 19:23:38 +02:00
if node . value ! = nil :
self . expression ( node . value )
2022-05-23 10:49:38 +02:00
self . emitByte ( OpCode . ReturnValue )
2022-05-16 19:23:38 +02:00
else :
self . emitByte ( OpCode . Return )
2022-04-04 12:29:23 +02:00
proc yieldStmt ( self : Compiler , node : YieldStmt ) =
## Compiles yield statements
self . expression ( node . expression )
self . emitByte ( OpCode . Yield )
proc raiseStmt ( self : Compiler , node : RaiseStmt ) =
## Compiles yield statements
self . expression ( node . exception )
self . emitByte ( OpCode . Raise )
proc continueStmt ( self : Compiler , node : ContinueStmt ) =
## Compiles continue statements. A continue statements
## jumps to the next iteration in a loop
if self . currentLoop . start < = 65535 :
self . emitByte ( Jump )
self . emitBytes ( self . currentLoop . start . toDouble ( ) )
else :
2022-05-07 10:48:01 +02:00
if self . currentLoop . start > 16777215 :
self . error ( " too much code to jump over in continue statement " )
2022-04-04 12:29:23 +02:00
self . emitByte ( LongJump )
self . emitBytes ( self . currentLoop . start . toTriple ( ) )
proc breakStmt ( self : Compiler , node : BreakStmt ) =
## Compiles break statements. A continue statement
## jumps to the next iteration in a loop
# Emits dummy jump offset, this is
# patched later
2022-05-26 18:31:40 +02:00
self . currentLoop . breakPos . add ( self . emitJump ( OpCode . Jump ) )
2022-04-04 12:29:23 +02:00
if self . currentLoop . depth > self . scopeDepth :
# Breaking out of a loop closes its scope
self . endScope ( )
proc patchBreaks ( self : Compiler ) =
## Patches "break" opcodes with
## actual jumps. This is needed
## because the size of code
## to skip is not known before
## the loop is fully compiled
for brk in self . currentLoop . breakPos :
self . chunk . code [ brk ] = JumpForwards . uint8 ( )
self . patchJump ( brk )
proc assertStmt ( self : Compiler , node : AssertStmt ) =
## Compiles assert statements (raise
## AssertionError if the expression is falsey)
self . expression ( node . expression )
self . emitByte ( OpCode . Assert )
2022-05-04 14:27:15 +02:00
proc statement ( self : Compiler , node : Statement ) =
2022-04-04 12:29:23 +02:00
## Compiles all statements
case node . kind :
of exprStmt :
2022-04-21 15:25:29 +02:00
var expression = ExprStmt ( node ) . expression
self . expression ( expression )
2022-05-18 13:32:32 +02:00
self . emitByte ( Pop ) # Expression statements discard their value. Their main use case is side effects in function calls
2022-04-04 12:29:23 +02:00
of NodeKind . ifStmt :
self . ifStmt ( IfStmt ( node ) )
of NodeKind . assertStmt :
self . assertStmt ( AssertStmt ( node ) )
of NodeKind . raiseStmt :
self . raiseStmt ( RaiseStmt ( node ) )
of NodeKind . breakStmt :
self . breakStmt ( BreakStmt ( node ) )
of NodeKind . continueStmt :
self . continueStmt ( ContinueStmt ( node ) )
of NodeKind . returnStmt :
self . returnStmt ( ReturnStmt ( node ) )
of NodeKind . importStmt :
discard
of NodeKind . whileStmt , NodeKind . forStmt :
## Our parser already desugars for loops to
## while loops!
let loop = self . currentLoop
self . currentLoop = Loop ( start : self . chunk . code . len ( ) ,
2022-05-07 10:48:01 +02:00
depth : self . scopeDepth , breakPos : @ [ ] )
2022-04-04 12:29:23 +02:00
self . whileStmt ( WhileStmt ( node ) )
self . patchBreaks ( )
self . currentLoop = loop
of NodeKind . forEachStmt :
discard
of NodeKind . blockStmt :
self . blockStmt ( BlockStmt ( node ) )
of NodeKind . yieldStmt :
self . yieldStmt ( YieldStmt ( node ) )
of NodeKind . awaitStmt :
self . awaitStmt ( AwaitStmt ( node ) )
of NodeKind . deferStmt :
self . deferStmt ( DeferStmt ( node ) )
of NodeKind . tryStmt :
discard
else :
2022-05-04 14:27:15 +02:00
self . expression ( Expression ( node ) )
2022-04-04 12:29:23 +02:00
2022-04-12 12:18:25 +02:00
proc varDecl ( self : Compiler , node : VarDecl ) =
## Compiles variable declarations
2022-05-29 17:04:19 +02:00
let expected = self . inferType ( node . valueType )
let actual = self . inferType ( node . value )
if expected = = nil and actual = = nil :
2022-05-25 12:15:45 +02:00
self . error ( & " ' {node.name.token.lexeme} ' has no type " )
2022-05-29 23:01:36 +02:00
elif expected ! = nil and expected . kind = = Mutable : # I mean, variables *are* already mutable (some of them anyway)
2022-05-29 17:04:19 +02:00
self . error ( & " invalid type ' {self.typeToStr(expected)} ' for var " )
elif not self . compareTypes ( expected , actual ) :
if expected ! = nil :
self . error ( & " expected value of type ' {self.typeToStr(expected)} ' , but ' {node.name.token.lexeme} ' is of type ' {self.typeToStr(actual)} ' " )
2022-04-12 12:18:25 +02:00
self . expression ( node . value )
2022-05-02 17:26:38 +02:00
self . declareName ( node )
2022-04-12 12:18:25 +02:00
2022-04-04 12:29:23 +02:00
proc funDecl ( self : Compiler , node : FunDecl ) =
## Compiles function declarations
2022-05-16 19:31:17 +02:00
# A function's code is just compiled linearly
# and then jumped over
2022-05-24 22:26:45 +02:00
let jmp = self . emitJump ( JumpForwards )
2022-05-22 15:26:12 +02:00
var function = self . currentFunction
2022-05-07 10:48:01 +02:00
self . declareName ( node )
2022-05-23 10:49:38 +02:00
self . frames . add ( self . names . high ( ) )
2022-05-22 15:26:12 +02:00
# TODO: Forward declarations
2022-05-07 10:48:01 +02:00
if node . body ! = nil :
2022-05-16 19:23:38 +02:00
if BlockStmt ( node . body ) . code . len ( ) = = 0 :
2022-05-25 12:15:45 +02:00
self . error ( " cannot declare function with empty body " )
2022-05-07 10:48:01 +02:00
let fnType = self . inferType ( node )
let impl = self . findByType ( node . name . token . lexeme , fnType )
if impl . len ( ) > 1 :
# Oh-oh! We found more than one implementation of
2022-05-25 12:15:45 +02:00
# the same function with the same name! Error!
2022-05-07 10:48:01 +02:00
var msg = & " multiple matching implementations of ' {node.name.token.lexeme} ' found: \n "
for fn in reversed ( impl ) :
2022-05-29 15:54:01 +02:00
msg & = & " - ' {fn.name} ' at line {fn.line} of type {self.typeToStr(fn.valueType)} \n "
2022-05-07 10:48:01 +02:00
self . error ( msg )
2022-05-22 15:26:12 +02:00
# We store the current function
self . currentFunction = node
# Since the deferred array is a linear
# sequence of instructions and we want
# to keep track to whose function's each
# set of deferred instruction belongs,
# we record the length of the deferred
# array before compiling the function
# and use this info later to compile
# the try/finally block with the deferred
# code
var deferStart = self . deferred . len ( )
2022-05-24 22:26:45 +02:00
# We let our debugger know a function is starting
2022-05-26 18:31:40 +02:00
let start = self . chunk . code . high ( )
2022-05-22 15:26:12 +02:00
self . blockStmt ( BlockStmt ( node . body ) )
# Yup, we're done. That was easy, huh?
2022-05-26 18:31:40 +02:00
# But, after all, functions are just named
2022-05-22 15:26:12 +02:00
# scopes, and we compile them just like that:
# we declare their name and arguments (before
# their body so recursion works) and then just
# handle them as a block statement (which takes
# care of incrementing self.scopeDepth so locals
# are resolved properly). There's a need for a bit
# of boilerplate code to make closures work, but
# that's about it
2022-05-24 22:26:45 +02:00
2022-05-22 15:26:12 +02:00
case self . currentFunction . kind :
of NodeKind . funDecl :
if not self . currentFunction . hasExplicitReturn :
let typ = self . inferType ( self . currentFunction )
2022-05-26 18:31:40 +02:00
if self . currentFunction . returnType = = nil and typ . returnType ! = nil :
2022-05-22 15:26:12 +02:00
self . error ( " non-empty return statement is not allowed in void functions " )
if self . currentFunction . returnType ! = nil :
2022-05-26 18:31:40 +02:00
self . error ( " function has an explicit return type, but no return statement was found " )
2022-05-22 15:26:12 +02:00
self . emitByte ( OpCode . Return )
of NodeKind . lambdaExpr :
if not LambdaExpr ( Declaration ( self . currentFunction ) ) . hasExplicitReturn :
self . emitByte ( OpCode . Return )
else :
discard # Unreachable
2022-05-27 14:01:57 +02:00
# Function is ending!
self . chunk . cfi . add ( start . toTriple ( ) )
self . chunk . cfi . add ( self . chunk . code . high ( ) . toTriple ( ) )
self . chunk . cfi . add ( self . frames [ ^ 1 ] . toTriple ( ) )
self . chunk . cfi . add ( uint8 ( node . arguments . len ( ) ) )
if not system . ` = = ` ( node . name , nil ) :
self . chunk . cfi . add ( node . name . token . lexeme . len ( ) . toDouble ( ) )
var s = node . name . token . lexeme
if node . name . token . lexeme . len ( ) > = uint16 . high ( ) . int :
s = node . name . token . lexeme [ 0 .. uint16 . high ( ) ]
self . chunk . cfi . add ( s . toBytes ( ) )
else :
self . chunk . cfi . add ( 0 . toDouble ( ) )
2022-05-22 15:26:12 +02:00
# Currently defer is not functional so we
# just pop the instructions
for i in countup ( deferStart , self . deferred . len ( ) - 1 , 1 ) :
self . deferred . delete ( i )
2022-04-04 12:29:23 +02:00
self . patchJump ( jmp )
# This makes us compile nested functions correctly
self . currentFunction = function
2022-05-24 22:26:45 +02:00
discard self . frames . pop ( )
2022-04-04 12:29:23 +02:00
2022-05-22 17:23:52 +02:00
proc patchReturnAddress ( self : Compiler , retAddr : int ) =
## Patches the return address of a function
## call. This is called at each iteration of
## the compiler's loop
let address = self . chunk . code . len ( ) . toQuad ( )
self . chunk . consts [ retAddr ] = address [ 0 ]
self . chunk . consts [ retAddr + 1 ] = address [ 1 ]
self . chunk . consts [ retAddr + 2 ] = address [ 2 ]
self . chunk . consts [ retAddr + 3 ] = address [ 3 ]
2022-04-04 12:29:23 +02:00
2022-05-04 14:27:15 +02:00
proc declaration ( self : Compiler , node : Declaration ) =
2022-04-04 12:29:23 +02:00
## Compiles all declarations
case node . kind :
of NodeKind . varDecl :
self . varDecl ( VarDecl ( node ) )
of NodeKind . funDecl :
self . funDecl ( FunDecl ( node ) )
else :
2022-05-04 14:27:15 +02:00
self . statement ( Statement ( node ) )
2022-04-04 12:29:23 +02:00
2022-05-18 13:32:32 +02:00
proc compile * ( self : Compiler , ast : seq [ Declaration ] , file : string ) : Chunk =
2022-04-04 12:29:23 +02:00
## Compiles a sequence of AST nodes into a chunk
## object
self . chunk = newChunk ( )
self . ast = ast
self . file = file
self . names = @ [ ]
self . scopeDepth = 0
self . currentFunction = nil
2022-05-22 13:02:48 +02:00
self . currentModule = self . file . extractFilename ( )
2022-04-04 12:29:23 +02:00
self . current = 0
2022-05-23 10:49:38 +02:00
self . frames = @ [ 0 ]
2022-04-04 12:29:23 +02:00
while not self . done ( ) :
2022-05-04 14:27:15 +02:00
self . declaration ( Declaration ( self . step ( ) ) )
2022-04-04 12:29:23 +02:00
if self . ast . len ( ) > 0 :
# *Technically* an empty program is a valid program
self . emitByte ( OpCode . Return ) # Exits the VM's main loop when used at the global scope
result = self . chunk
2022-05-20 15:47:04 +02:00
if self . ast . len ( ) > 0 and self . scopeDepth ! = 0 :
self . error ( & " invalid state: invalid scopeDepth value (expected 0, got {self.scopeDepth}), did you forget to call endScope/beginScope? " )
2022-05-29 17:04:19 +02:00
self . endScope ( )