diff --git a/src/backend/compiler.nim b/src/backend/compiler.nim index 00f9ea8..b457ba4 100644 --- a/src/backend/compiler.nim +++ b/src/backend/compiler.nim @@ -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 = diff --git a/src/backend/meta/errors.nim b/src/backend/meta/errors.nim new file mode 100644 index 0000000..3b33b25 --- /dev/null +++ b/src/backend/meta/errors.nim @@ -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 \ No newline at end of file diff --git a/src/backend/parser.nim b/src/backend/parser.nim index 3e8f05d..a7166d2 100644 --- a/src/backend/parser.nim +++ b/src/backend/parser.nim @@ -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())