Merge remote-tracking branch 'upstream/master'

This commit is contained in:
Productive2 2021-02-20 14:10:42 +01:00
commit 3f0ae9bc1b
3 changed files with 11 additions and 12 deletions

View File

@ -896,7 +896,7 @@ proc parseFunction(self: Compiler, funType: FunctionType) =
self.function.arity -= 1
self.function.optionals += 1
self.expression()
self.function.defaults.add(self.parser.previous.lexeme)
self.function.defaults.append(self.parser.previous.lexeme.asStr())
defaultFollows = true
elif defaultFollows:
self.compileError("non-default argument follows default argument")
@ -1013,9 +1013,10 @@ proc statement(self: Compiler) =
proc declaration(self: Compiler) =
## Parses declarations
if self.afterReturn:
self.compileError("dead code after return statement")
self.parser.tokens.append(Token(kind: TokenType.EOF, lexeme: ""))
# TODO -> Fix this
# if self.afterReturn:
# self.compileError("dead code after return statement")
# self.parser.tokens.append(Token(kind: TokenType.EOF, lexeme: ""))
if self.parser.match(FUN):
self.funDeclaration()
elif self.parser.match(VAR):

View File

@ -19,7 +19,7 @@ import ../types/baseObject
import ../types/arraylist
type
Chunk* = ref object
Chunk* = object
## A piece of bytecode.
## Consts represents the constants table the code is referring to
## Code is the compiled bytecode
@ -72,7 +72,7 @@ type
As
const simpleInstructions* = {OpCode.Return, OpCode.Add, OpCode.Multiply,
OpCode.Divide, OpCode.Subtract,
OpCode.Mod, OpCode.Pow, OpCode.Nil,
@ -112,5 +112,3 @@ proc addConstant*(self: Chunk, constant: ptr Obj): array[3, uint8] =
self.consts.append(constant)
let index = self.consts.high()
result = cast[array[3, uint8]](index)

View File

@ -1,6 +1,7 @@
import baseObject
import ../meta/opcode
import japlString
import arraylist
type
@ -19,7 +20,7 @@ type
name*: ptr String
arity*: int # The number of required parameters
optionals*: int # The number of optional parameters
defaults*: seq[string]
defaults*: ptr ArrayList[ptr String]
chunk*: Chunk # The function's body
@ -28,7 +29,6 @@ proc newFunction*(name: string = "", chunk: Chunk, arity: int = 0): ptr Function
## bytecode chunk and arity. If the name is an empty string
## (the default), the function will be an
## anonymous code object
# TODO: Add lambdas
# TODO: Add support for optional parameters
result = allocateObj(Function, ObjectType.Function)
if name.len > 1:
@ -38,12 +38,12 @@ proc newFunction*(name: string = "", chunk: Chunk, arity: int = 0): ptr Function
result.arity = arity
result.chunk = chunk
result.optionals = 0 # TODO
result.defaults = newArrayList[ptr String]()
proc newLambda*(chunk: Chunk, arity: int = 0): ptr Function =
## Allocates a new lambda object (anonymous function) with the given
## bytecode chunk and arity
# TODO: Add lambdas
# TODO: Add support for optional parameters
result = allocateObj(Function, ObjectType.Function)
result.name = "<lambda function>".asStr()
@ -76,4 +76,4 @@ proc hash*(self: ptr Function): uint64 =
proc eq*(self, other: ptr Function): bool =
result = self == other # Pointer equality
result = self == other # Pointer equality