Parser bugfixes + small changes in initializer methods for parser and compiler. Added generic error type for NimVM parsing exceptions

This commit is contained in:
Nocturn9x 2021-10-23 09:11:36 +02:00
parent 4ad34f30e4
commit 7237141ec1
3 changed files with 26 additions and 14 deletions

View File

@ -30,20 +30,19 @@ type
ast: seq[ASTNode]
current: int
file: string
# Keeps track of all identifiers
# in the code
names: seq[IdentifierWrapper]
CompileError* = object of NimVMException
proc initCompiler*(self: Compiler = nil, ast: seq[ASTNode], file: string): Compiler =
## Initializes a new Compiler object or
## resets the state of an existing one
if self != nil:
result = self
self.ast = ast
self.current = 0
self.file = file
self.names = @[]
proc initCompiler*(): Compiler =
## Initializes a new Compiler object
result.ast = @[]
result.current = 0
result.file = ""
result.names = @[]
proc peek(self: Compiler, distance: int = 0): ASTNode =

View File

@ -0,0 +1,15 @@
# Copyright 2020 Mattia Giambirtone
#
# 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.
type NimVMException* = ref object of CatchableError

View File

@ -37,11 +37,8 @@ type
## A parse error
proc initParser*(self: Parser = nil): Parser =
proc initParser*(): Parser =
## Initializes a new Parser object
## or resets an already existing one
if self != nil:
result = self
new(result)
result.current = 0
result.file = ""
@ -796,8 +793,9 @@ proc declaration(self: Parser): ASTNode =
proc parse*(self: Parser, tokens: seq[Token], file: string): seq[ASTNode] =
## Parses a series of tokens into an AST node
discard self.initParser()
self.tokens = tokens
self.file = file
self.current = 0
self.context = Script
while not self.done():
result.add(self.declaration())