874 lines
28 KiB
Nim
874 lines
28 KiB
Nim
# Copyright 2024 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.
|
|
|
|
## An Abstract Syntax Tree (AST) structure for our recursive-descent
|
|
## top-down parser. For more info, check out docs/grammar.md
|
|
|
|
|
|
import std/strformat
|
|
import std/strutils
|
|
import std/tables
|
|
|
|
|
|
import token
|
|
export token
|
|
|
|
|
|
type
|
|
NodeKind* = enum
|
|
## Enumeration of the AST
|
|
## node types
|
|
|
|
# Declarations
|
|
typeDecl = 0'u8
|
|
funDecl,
|
|
varDecl,
|
|
# Statements
|
|
ifStmt,
|
|
returnStmt,
|
|
breakStmt,
|
|
continueStmt,
|
|
whileStmt,
|
|
forEachStmt,
|
|
blockStmt,
|
|
namedBlockStmt,
|
|
raiseStmt,
|
|
assertStmt,
|
|
tryStmt,
|
|
yieldStmt,
|
|
awaitStmt,
|
|
importStmt,
|
|
exportStmt,
|
|
deferStmt,
|
|
# An expression followed by a semicolon
|
|
exprStmt,
|
|
# Expressions
|
|
assignExpr,
|
|
lambdaExpr,
|
|
awaitExpr,
|
|
yieldExpr,
|
|
binaryExpr,
|
|
unaryExpr,
|
|
sliceExpr,
|
|
callExpr,
|
|
getterExpr, # Get expressions like a.b
|
|
setterExpr, # Set expressions like a.b = "c"
|
|
# Primary expressions
|
|
groupingExpr, # Parenthesized expressions such as (true) and (3 + 4)
|
|
trueExpr,
|
|
falseExpr,
|
|
strExpr,
|
|
charExpr,
|
|
intExpr,
|
|
floatExpr,
|
|
hexExpr,
|
|
octExpr,
|
|
binExpr,
|
|
nanExpr,
|
|
infExpr,
|
|
identExpr, # Identifier
|
|
pragmaExpr,
|
|
refExpr,
|
|
ptrExpr,
|
|
genericExpr,
|
|
switchStmt,
|
|
lentExpr,
|
|
constExpr
|
|
|
|
# Here I would've rather used object variants, and in fact that's what was in
|
|
# place before, but not being able to re-declare a field of the same type in
|
|
# another case branch is kind of a deal breaker long-term, so until that is
|
|
# fixed (check out https://github.com/nim-lang/RFCs/issues/368 for more info)
|
|
# I'll stick to using inheritance instead
|
|
|
|
|
|
ASTNode* = ref object of RootObj
|
|
## An AST node
|
|
kind*: NodeKind
|
|
# Regardless of the type of node, we keep the token in the AST node for internal usage.
|
|
# This is not shown when the node is printed, but makes it a heck of a lot easier to report
|
|
# errors accurately even deep in the compilation pipeline
|
|
token*: Token
|
|
file*: string
|
|
|
|
# A bunch of aliases and structures for easier typing
|
|
Parameter* = ref object of RootObj
|
|
ident*: IdentExpr
|
|
valueType*: Expression
|
|
default*: Expression
|
|
|
|
Parameters* = OrderedTableRef[string, Parameter]
|
|
|
|
TypeField* = ref object of Parameter
|
|
isPrivate*: bool
|
|
|
|
TypeFields* = OrderedTableRef[string, TypeField]
|
|
|
|
TypeGeneric* = ref object
|
|
ident*: IdentExpr
|
|
constr*: Expression
|
|
|
|
TypeGenerics* = OrderedTableRef[string, TypeGeneric]
|
|
|
|
Declaration* = ref object of ASTNode
|
|
## A declaration
|
|
name*: IdentExpr
|
|
isPrivate*: bool
|
|
pragmas*: seq[Pragma]
|
|
genericTypes*: TypeGenerics
|
|
genericValues*: TypeGenerics
|
|
|
|
Statement* = ref object of ASTNode
|
|
## A statement
|
|
|
|
Expression* = ref object of ASTNode
|
|
## An expression
|
|
|
|
LiteralExpr* = ref object of Expression
|
|
## A literal expression (like a number or a string)
|
|
literal*: Token
|
|
|
|
IdentExpr* = ref object of Expression
|
|
## An identifier expression
|
|
name*: Token
|
|
|
|
GroupingExpr* = ref object of Expression
|
|
## A parenthesized (or "grouped") expression
|
|
expression*: Expression
|
|
|
|
GetterExpr* = ref object of Expression
|
|
## A getter expression (e.g. "a.b")
|
|
obj*: Expression
|
|
name*: IdentExpr
|
|
|
|
SetterExpr* = ref object of GetterExpr
|
|
## A setter expression (e.g. "a.b = c")
|
|
|
|
# Since a setter expression is just
|
|
# a getter one followed by an assignment,
|
|
# inheriting from it makes sense
|
|
value*: Expression
|
|
|
|
CallExpr* = ref object of Expression
|
|
## A call expression
|
|
callee*: Expression # The object being called
|
|
arguments*: tuple[positionals: seq[Expression], keyword: TableRef[string, tuple[name: IdentExpr, value: Expression]]]
|
|
closeParen*: Token # Needed for error reporting
|
|
|
|
GenericExpr* = ref object of Expression
|
|
## A generic instantiation expression
|
|
ident*: IdentExpr
|
|
args*: seq[Expression]
|
|
|
|
UnaryExpr* = ref object of Expression
|
|
## A unary expression
|
|
operator*: Token
|
|
a*: Expression
|
|
|
|
BinaryExpr* = ref object of UnaryExpr
|
|
## A binary expression
|
|
|
|
# Binary expressions can be seen here as unary
|
|
# expressions with an extra operand, so we just
|
|
# inherit from that and add it
|
|
b*: Expression
|
|
|
|
LambdaExpr* = ref object of Expression
|
|
## A lambda expression. This is basically
|
|
## a mirror of FunDecl without a name
|
|
body*: Statement
|
|
parameters*: Parameters
|
|
returnType*: Expression
|
|
pragmas*: seq[Pragma]
|
|
genericTypes*: TypeGenerics
|
|
genericValues*: TypeGenerics
|
|
|
|
SliceExpr* = ref object of Expression
|
|
## A slice expression such as x[b]
|
|
expression*: Expression
|
|
elements*: seq[Expression]
|
|
|
|
AssignExpr* = ref object of Expression
|
|
## An assignment expression such
|
|
## as x = y
|
|
name*: IdentExpr
|
|
value*: Expression
|
|
|
|
ExprStmt* = ref object of Statement
|
|
## An expression followed by a semicolon
|
|
expression*: Expression
|
|
|
|
ImportStmt* = ref object of Statement
|
|
## An import statement
|
|
moduleName*: IdentExpr
|
|
names*: seq[IdentExpr]
|
|
|
|
ExportStmt* = ref object of Statement
|
|
## An export statement
|
|
name*: IdentExpr
|
|
|
|
AssertStmt* = ref object of Statement
|
|
## An assert statement
|
|
expression*: Expression
|
|
|
|
BlockStmt* = ref object of Statement
|
|
## A block statement. This is basically
|
|
## the equivalent of a C scope
|
|
|
|
|
|
body*: seq[ASTNode]
|
|
|
|
NamedBlockStmt* = ref object of BlockStmt
|
|
## Identical to a block statement, except
|
|
## it also has a name which can be used to
|
|
## jump from/to it using break and continue
|
|
## statements
|
|
name*: IdentExpr
|
|
|
|
ForEachStmt* = ref object of Statement
|
|
## A foreach statement
|
|
identifier*: IdentExpr
|
|
expression*: Expression
|
|
body*: Statement
|
|
|
|
WhileStmt* = ref object of Statement
|
|
## A C-style while statement
|
|
condition*: Expression
|
|
body*: BlockStmt
|
|
|
|
BreakStmt* = ref object of Statement
|
|
## A break statement
|
|
label*: IdentExpr # Optional: which named block should we jump out of?
|
|
|
|
ContinueStmt* = ref object of Statement
|
|
## A continue statement
|
|
label*: IdentExpr # Optional: which named block should we jump to the start of?
|
|
|
|
ReturnStmt* = ref object of Statement
|
|
## A return statement
|
|
value*: Expression # Optional
|
|
|
|
IfStmt* = ref object of Statement
|
|
## An if statement
|
|
condition*: Expression
|
|
thenBranch*: Statement
|
|
elseBranch*: Statement # Optional
|
|
|
|
VarDecl* = ref object of Declaration
|
|
## A variable declaration
|
|
|
|
constant*: bool # Is this a constant?
|
|
mutable*: bool # Is the value mutable?
|
|
valueType*: Expression
|
|
value*: Expression
|
|
|
|
FunDecl* = ref object of Declaration
|
|
## A function declaration
|
|
body*: Statement
|
|
parameters*: Parameters
|
|
returnType*: Expression
|
|
|
|
TypeDecl* = ref object of Declaration
|
|
## A type declaration
|
|
|
|
# Empty if type is an enum:
|
|
# contains all fields of the
|
|
# structure
|
|
fields*: TypeFields
|
|
# Empty if type is a structure:
|
|
# contains all enum members (they
|
|
# are separate from fields because
|
|
# they are all their own type of sorts)
|
|
members*: seq[TypeDecl]
|
|
isEnum*: bool
|
|
isRef*: bool # Is this type a managed reference?
|
|
parent*: Expression
|
|
# Only filled if it's a type alias
|
|
value*: Expression
|
|
|
|
Pragma* = ref object of Expression
|
|
name*: IdentExpr
|
|
args*: seq[LiteralExpr]
|
|
|
|
Var* = ref object of Expression
|
|
value*: Expression
|
|
|
|
Ref* = ref object of Expression
|
|
value*: Expression
|
|
|
|
Ptr* = ref object of Expression
|
|
value*: Expression
|
|
|
|
Lent* = ref object of Expression
|
|
value*: Expression
|
|
|
|
Const* = ref object of Expression
|
|
value*: Expression
|
|
|
|
SwitchStmt* = ref object of Statement
|
|
switch*: Expression
|
|
branches*: seq[tuple[cond: Expression, body: BlockStmt]]
|
|
default*: BlockStmt
|
|
|
|
|
|
proc isLiteral*(self: ASTNode): bool =
|
|
## Returns whether the given AST node
|
|
## represents a literal, constant expression
|
|
return self.kind in [intExpr, hexExpr, binExpr, octExpr, strExpr, falseExpr, trueExpr, floatExpr, nanExpr, infExpr]
|
|
|
|
|
|
proc isConst*(self: ASTNode): bool =
|
|
## Returns true if the given
|
|
## AST node represents a value
|
|
## of a constant type. All integers,
|
|
## strings and singletons count as
|
|
## constants
|
|
return self.isLiteral() # TODO
|
|
|
|
|
|
proc isDecl*(self: ASTNode): bool =
|
|
## Returns true if the given AST node
|
|
## represents a declaration
|
|
return self.kind in [NodeKind.typeDecl, NodeKind.funDecl, NodeKind.varDecl]
|
|
|
|
|
|
## AST node constructors
|
|
proc newASTNode*(kind: NodeKind, token: Token): ASTNode =
|
|
## Initializes a new generic ASTNode object
|
|
new(result)
|
|
result.kind = kind
|
|
result.token = token
|
|
|
|
|
|
proc newPragma*(name: IdentExpr, args: seq[LiteralExpr]): Pragma =
|
|
new(result)
|
|
result.kind = NodeKind.pragmaExpr
|
|
result.args = args
|
|
result.name = name
|
|
result.token = name.token
|
|
|
|
|
|
proc newRefExpr*(expression: Expression, token: Token): Ref =
|
|
new(result)
|
|
result.kind = NodeKind.refExpr
|
|
result.value = expression
|
|
result.token = token
|
|
|
|
|
|
proc newPtrExpr*(expression: Expression, token: Token): Ptr =
|
|
new(result)
|
|
result.kind = NodeKind.ptrExpr
|
|
result.value = expression
|
|
result.token = token
|
|
|
|
|
|
proc newLentExpr*(expression: Expression, token: Token): Lent =
|
|
new(result)
|
|
result.kind = NodeKind.lentExpr
|
|
result.value = expression
|
|
result.token = token
|
|
|
|
|
|
proc newConstExpr*(expression: Expression, token: Token): Const =
|
|
new(result)
|
|
result.kind = NodeKind.constExpr
|
|
result.value = expression
|
|
result.token = token
|
|
|
|
|
|
|
|
proc newSwitchStmt*(switch: Expression, branches: seq[tuple[cond: Expression, body: BlockStmt]], default: BlockStmt, token: Token): SwitchStmt =
|
|
new(result)
|
|
result.kind = NodeKind.switchStmt
|
|
result.switch = switch
|
|
result.branches = branches
|
|
result.token = token
|
|
result.default = default
|
|
|
|
|
|
proc newIntExpr*(literal: Token): LiteralExpr =
|
|
new(result)
|
|
result.kind = NodeKind.intExpr
|
|
result.literal = literal
|
|
result.token = literal
|
|
|
|
|
|
proc newOctExpr*(literal: Token): LiteralExpr =
|
|
new(result)
|
|
result.kind = NodeKind.octExpr
|
|
result.literal = literal
|
|
result.token = literal
|
|
|
|
|
|
proc newHexExpr*(literal: Token): LiteralExpr =
|
|
new(result)
|
|
result.kind = NodeKind.hexExpr
|
|
result.literal = literal
|
|
result.token = literal
|
|
|
|
|
|
proc newBinExpr*(literal: Token): LiteralExpr =
|
|
new(result)
|
|
result.kind = NodeKind.binExpr
|
|
result.literal = literal
|
|
result.token = literal
|
|
|
|
|
|
proc newFloatExpr*(literal: Token): LiteralExpr =
|
|
new(result)
|
|
result.kind = NodeKind.floatExpr
|
|
result.literal = literal
|
|
result.token = literal
|
|
|
|
|
|
proc newTrueExpr*(token: Token): LiteralExpr = LiteralExpr(kind: NodeKind.trueExpr, token: token, literal: token)
|
|
proc newFalseExpr*(token: Token): LiteralExpr = LiteralExpr(kind: NodeKind.falseExpr, token: token, literal: token)
|
|
proc newNanExpr*(token: Token): LiteralExpr = LiteralExpr(kind: NodeKind.nanExpr, token: token, literal: token)
|
|
proc newInfExpr*(token: Token): LiteralExpr = LiteralExpr(kind: NodeKind.infExpr, token: token, literal: token)
|
|
|
|
|
|
proc newStrExpr*(literal: Token): LiteralExpr =
|
|
new(result)
|
|
result.kind = NodeKind.strExpr
|
|
result.literal = literal
|
|
result.token = literal
|
|
|
|
|
|
proc newCharExpr*(literal: Token): LiteralExpr =
|
|
new(result)
|
|
result.kind = NodeKind.charExpr
|
|
result.literal = literal
|
|
result.token = literal
|
|
|
|
|
|
proc newIdentExpr*(name: Token): IdentExpr =
|
|
new(result)
|
|
result.kind = NodeKind.identExpr
|
|
result.name = name
|
|
result.token = name
|
|
|
|
|
|
proc newGroupingExpr*(expression: Expression, token: Token): GroupingExpr =
|
|
new(result)
|
|
result.kind = NodeKind.groupingExpr
|
|
result.expression = expression
|
|
result.token = token
|
|
|
|
|
|
proc newLambdaExpr*(parameters: Parameters = newOrderedTable[string, Parameter](),
|
|
body: Statement = nil, token: Token = nil, pragmas: seq[Pragma] = @[],
|
|
returnType: Expression = nil, genericTypes: TypeGenerics = newOrderedTable[string, TypeGeneric](),
|
|
genericValues: TypeGenerics = newOrderedTable[string, TypeGeneric]()): LambdaExpr =
|
|
new(result)
|
|
result.kind = NodeKind.lambdaExpr
|
|
result.body = body
|
|
result.parameters = parameters
|
|
result.token = token
|
|
result.returnType = returnType
|
|
result.pragmas = pragmas
|
|
result.genericTypes = genericTypes
|
|
result.genericValues = genericValues
|
|
|
|
|
|
proc newGetterExpr*(obj: Expression, name: IdentExpr, token: Token): GetterExpr =
|
|
new(result)
|
|
result.kind = NodeKind.getterExpr
|
|
result.obj = obj
|
|
result.name = name
|
|
result.token = token
|
|
|
|
|
|
proc newSetterExpr*(obj: Expression, name: IdentExpr, value: Expression, token: Token): SetterExpr =
|
|
new(result)
|
|
result.kind = NodeKind.setterExpr
|
|
result.obj = obj
|
|
result.name = name
|
|
result.value = value
|
|
result.token = token
|
|
|
|
|
|
proc newCallExpr*(callee: Expression, arguments: tuple[positionals: seq[
|
|
Expression], keyword: TableRef[string, tuple[name: IdentExpr, value: Expression]]],
|
|
token: Token): CallExpr =
|
|
new(result)
|
|
result.kind = NodeKind.callExpr
|
|
result.callee = callee
|
|
result.arguments = arguments
|
|
result.token = token
|
|
|
|
|
|
proc newGenericExpr*(ident: IdentExpr, args: seq[Expression]): GenericExpr =
|
|
new(result)
|
|
result.kind = NodeKind.genericExpr
|
|
result.ident = ident
|
|
result.args = args
|
|
result.token = ident.token
|
|
|
|
|
|
proc newSliceExpr*(expression: Expression, elements: seq[Expression], token: Token): SliceExpr =
|
|
new(result)
|
|
result.kind = NodeKind.sliceExpr
|
|
result.expression = expression
|
|
result.elements = elements
|
|
result.token = token
|
|
|
|
|
|
proc newUnaryExpr*(operator: Token, a: Expression): UnaryExpr =
|
|
new(result)
|
|
result.kind = NodeKind.unaryExpr
|
|
result.operator = operator
|
|
result.a = a
|
|
result.token = result.operator
|
|
|
|
|
|
proc newBinaryExpr*(a: Expression, operator: Token, b: Expression): BinaryExpr =
|
|
new(result)
|
|
result.kind = NodeKind.binaryExpr
|
|
result.operator = operator
|
|
result.a = a
|
|
result.b = b
|
|
result.token = operator
|
|
|
|
|
|
proc newAssignExpr*(name: IdentExpr, value: Expression,
|
|
token: Token): AssignExpr =
|
|
new(result)
|
|
result.kind = NodeKind.assignExpr
|
|
result.name = name
|
|
result.value = value
|
|
result.token = token
|
|
|
|
|
|
proc newExprStmt*(expression: Expression, token: Token): ExprStmt =
|
|
new(result)
|
|
result.kind = NodeKind.exprStmt
|
|
result.expression = expression
|
|
result.token = token
|
|
|
|
|
|
proc newImportStmt*(moduleName: IdentExpr, names: seq[IdentExpr], token: Token): ImportStmt =
|
|
new(result)
|
|
result.kind = NodeKind.importStmt
|
|
result.moduleName = moduleName
|
|
result.token = token
|
|
result.names = names
|
|
|
|
|
|
proc newExportStmt*(name: IdentExpr, token: Token): ExportStmt =
|
|
new(result)
|
|
result.kind = NodeKind.exportStmt
|
|
result.name = name
|
|
result.token = token
|
|
|
|
|
|
proc newAssertStmt*(expression: Expression, token: Token): AssertStmt =
|
|
new(result)
|
|
result.kind = NodeKind.assertStmt
|
|
result.expression = expression
|
|
result.token = token
|
|
|
|
|
|
proc newBlockStmt*(body: seq[ASTNode], token: Token): BlockStmt =
|
|
new(result)
|
|
result.kind = NodeKind.blockStmt
|
|
result.body = body
|
|
result.token = token
|
|
|
|
|
|
proc newNamedBlockStmt*(body: seq[ASTNode], name: IdentExpr, token: Token): NamedBlockStmt =
|
|
new(result)
|
|
result.kind = NodeKind.namedBlockStmt
|
|
result.body = body
|
|
result.token = token
|
|
result.name = name
|
|
|
|
|
|
proc newWhileStmt*(condition: Expression, body: BlockStmt, token: Token): WhileStmt =
|
|
new(result)
|
|
result.kind = NodeKind.whileStmt
|
|
result.condition = condition
|
|
result.body = body
|
|
result.token = token
|
|
|
|
|
|
proc newForEachStmt*(identifier: IdentExpr, expression: Expression, body: Statement, token: Token): ForEachStmt =
|
|
new(result)
|
|
result.kind = NodeKind.forEachStmt
|
|
result.identifier = identifier
|
|
result.expression = expression
|
|
result.body = body
|
|
result.token = token
|
|
|
|
|
|
proc newBreakStmt*(token: Token, label: IdentExpr = nil): BreakStmt =
|
|
new(result)
|
|
result.kind = NodeKind.breakStmt
|
|
result.token = token
|
|
result.label = label
|
|
|
|
|
|
proc newContinueStmt*(token: Token, label: IdentExpr = nil): ContinueStmt =
|
|
new(result)
|
|
result.kind = NodeKind.continueStmt
|
|
result.token = token
|
|
result.label = label
|
|
|
|
|
|
proc newReturnStmt*(value: Expression, token: Token): ReturnStmt =
|
|
new(result)
|
|
result.kind = NodeKind.returnStmt
|
|
result.value = value
|
|
result.token = token
|
|
|
|
|
|
proc newIfStmt*(condition: Expression, thenBranch, elseBranch: Statement, token: Token): IfStmt =
|
|
new(result)
|
|
result.kind = NodeKind.ifStmt
|
|
result.condition = condition
|
|
result.thenBranch = thenBranch
|
|
result.elseBranch = elseBranch
|
|
result.token = token
|
|
|
|
|
|
proc newVarDecl*(name: IdentExpr, valueType, value: Expression, token: Token,
|
|
constant: bool = false, isPrivate: bool = true, mutable: bool = false,
|
|
pragmas: seq[Pragma] = @[]): VarDecl =
|
|
result = VarDecl(kind: varDecl)
|
|
result.name = name
|
|
result.valueType = valueType
|
|
result.value = value
|
|
result.constant = constant
|
|
result.isPrivate = isPrivate
|
|
result.token = token
|
|
result.mutable = mutable
|
|
result.pragmas = pragmas
|
|
|
|
|
|
proc newFunDecl*(name: IdentExpr, parameters: Parameters, body: Statement, isPrivate: bool, token: Token, pragmas: seq[Pragma] = @[],
|
|
returnType: Expression, genericTypes: TypeGenerics = newOrderedTable[string, TypeGeneric](),
|
|
genericValues: TypeGenerics = newOrderedTable[string, TypeGeneric]()): FunDecl =
|
|
new(result)
|
|
result.kind = NodeKind.funDecl
|
|
result.name = name
|
|
result.parameters = parameters
|
|
result.body = body
|
|
result.isPrivate = isPrivate
|
|
result.token = token
|
|
result.pragmas = pragmas
|
|
result.returnType = returnType
|
|
result.genericTypes = genericTypes
|
|
result.genericValues = genericValues
|
|
|
|
|
|
proc newTypeDecl*(name: IdentExpr, fields: TypeFields, isPrivate: bool, token: Token, pragmas: seq[Pragma], parent: IdentExpr, isEnum: bool, isRef: bool,
|
|
genericTypes: TypeGenerics = newOrderedTable[string, TypeGeneric](), genericValues: TypeGenerics = newOrderedTable[string, TypeGeneric]()): TypeDecl =
|
|
new(result)
|
|
result.kind = NodeKind.typeDecl
|
|
result.name = name
|
|
result.fields = fields
|
|
result.isPrivate = isPrivate
|
|
result.token = token
|
|
result.pragmas = pragmas
|
|
result.genericTypes = genericTypes
|
|
result.genericValues = genericValues
|
|
result.parent = parent
|
|
result.isEnum = isEnum
|
|
result.isRef = isRef
|
|
result.members = @[]
|
|
|
|
|
|
proc `$`*(self: Parameter): string
|
|
proc `$`*(self: TypeField): string
|
|
proc `$`*(self: TypeGeneric): string
|
|
|
|
|
|
proc `$`*(self: ASTNode): string =
|
|
if self.isNil():
|
|
return "nil"
|
|
case self.kind:
|
|
of intExpr, floatExpr, hexExpr, binExpr, octExpr, strExpr, trueExpr,
|
|
falseExpr, infExpr, nanExpr:
|
|
if self.kind in {trueExpr, falseExpr, infExpr, nanExpr}:
|
|
result &= &"Literal({($self.kind)[0..^5]})"
|
|
elif self.kind == strExpr:
|
|
result &= &"Literal({LiteralExpr(self).literal.lexeme[1..^2].escape()})"
|
|
else:
|
|
result &= &"Literal({LiteralExpr(self).literal.lexeme})"
|
|
of identExpr:
|
|
result &= &"Identifier('{IdentExpr(self).name.lexeme}')"
|
|
of groupingExpr:
|
|
result &= &"Grouping({GroupingExpr(self).expression})"
|
|
of getterExpr:
|
|
var self = GetterExpr(self)
|
|
result &= &"Getter(obj={self.obj}, name={self.name})"
|
|
of setterExpr:
|
|
var self = SetterExpr(self)
|
|
result &= &"Setter(obj={self.obj}, name={self.value}, value={self.value})"
|
|
of callExpr:
|
|
var self = CallExpr(self)
|
|
result &= &"""Call({self.callee}, arguments=(positionals={self.arguments.positionals}, keyword={self.arguments.keyword}))"""
|
|
of unaryExpr:
|
|
var self = UnaryExpr(self)
|
|
result &= &"Unary(Operator('{self.operator.lexeme}'), {self.a})"
|
|
of binaryExpr:
|
|
var self = BinaryExpr(self)
|
|
result &= &"Binary({self.a}, Operator('{self.operator.lexeme}'), {self.b})"
|
|
of assignExpr:
|
|
var self = AssignExpr(self)
|
|
result &= &"Assign(name={self.name}, value={self.value})"
|
|
of exprStmt:
|
|
var self = ExprStmt(self)
|
|
result &= &"ExpressionStatement({self.expression})"
|
|
of breakStmt:
|
|
var self = BreakStmt(self)
|
|
if self.label.isNil:
|
|
result = "Break()"
|
|
else:
|
|
result = &"Break({self.label})"
|
|
of importStmt:
|
|
var self = ImportStmt(self)
|
|
result &= &"Import({self.moduleName}, names={self.names})"
|
|
of assertStmt:
|
|
var self = AssertStmt(self)
|
|
result &= &"Assert({self.expression})"
|
|
of blockStmt:
|
|
var self = BlockStmt(self)
|
|
result &= &"""Block([{self.body.join(", ")}])"""
|
|
of namedBlockStmt:
|
|
var self = NamedBlockStmt(self)
|
|
result &= &"""Block(name={self.name}, [{self.body.join(", ")}])"""
|
|
of whileStmt:
|
|
var self = WhileStmt(self)
|
|
result &= &"While(condition={self.condition}, body={self.body})"
|
|
of forEachStmt:
|
|
var self = ForEachStmt(self)
|
|
result &= &"ForEach(identifier={self.identifier}, expression={self.expression}, body={self.body})"
|
|
of returnStmt:
|
|
var self = ReturnStmt(self)
|
|
result &= &"Return({self.value})"
|
|
of ifStmt:
|
|
var self = IfStmt(self)
|
|
if self.elseBranch == nil:
|
|
result &= &"If(condition={self.condition}, thenBranch={self.thenBranch}, elseBranch=nil)"
|
|
else:
|
|
result &= &"If(condition={self.condition}, thenBranch={self.thenBranch}, elseBranch={self.elseBranch})"
|
|
of varDecl:
|
|
var self = VarDecl(self)
|
|
result &= &"Var(name={self.name}, type={self.valueType}, value={self.value}, mutable={self.mutable}, constant={self.constant}, private={self.isPrivate}, pragmas={self.pragmas})"
|
|
of funDecl:
|
|
var self = FunDecl(self)
|
|
result &= &"""FunDecl(name={self.name}, body={self.body}, returnType={self.returnType}, parameters={self.parameters}, genericTypes={self.genericTypes}, genericValues={self.genericValues}, private={self.isPrivate}, pragmas={self.pragmas})"""
|
|
of typeDecl:
|
|
var self = TypeDecl(self)
|
|
result &= &"""TypeDecl(name={self.name}, fields={self.fields}, members={self.members}, private={self.isPrivate}, pragmas={self.pragmas}, genericTypes={self.genericTypes}, genericValues={self.genericValues}, parent={self.parent}, ref={self.isRef}, enum={self.isEnum}, value={self.value})"""
|
|
of lambdaExpr:
|
|
var self = LambdaExpr(self)
|
|
result &= &"""Lambda(body={self.body}, returnType={self.returnType}, parameters={self.parameters}, pragmas={self.pragmas})"""
|
|
of sliceExpr:
|
|
var self = SliceExpr(self)
|
|
result &= &"""Slice({self.expression}, elements=[{self.elements.join(", ")}])"""
|
|
of pragmaExpr:
|
|
var self = Pragma(self)
|
|
result &= &"Pragma(name={self.name}, args={self.args})"
|
|
of refExpr:
|
|
result &= &"Ref({Ref(self).value})"
|
|
of ptrExpr:
|
|
result &= &"Ptr({Ptr(self).value})"
|
|
of constExpr:
|
|
result &= &"Const({Const(self).value})"
|
|
of lentExpr:
|
|
result &= &"Lent({Lent(self).value})"
|
|
of genericExpr:
|
|
var self = GenericExpr(self)
|
|
result &= &"Generic(ident={self.ident}, args={self.args})"
|
|
else:
|
|
discard
|
|
|
|
proc `$`*(self: Parameter): string = &"Parameter(name={self.ident}, type={self.valueType}, default={self.default})"
|
|
proc `$`*(self: TypeField): string = &"Field(name={self.ident}, type={self.valueType}, default={self.default}, private={self.isPrivate})"
|
|
proc `$`*(self: TypeGeneric): string = &"Parameter(name={self.ident}, constraint={self.constr})"
|
|
|
|
|
|
proc `==`*(self, other: IdentExpr): bool {.inline.} = self.token == other.token
|
|
|
|
|
|
proc getRelativeBoundaries*(self: ASTNode): tuple[start, stop: int] =
|
|
## Recursively computes the position of a node relative
|
|
## to its containing line
|
|
case self.kind:
|
|
of NodeKind.varDecl:
|
|
var self = VarDecl(self)
|
|
let start = self.token.relPos.start
|
|
var stop = self.name.token.relPos.stop
|
|
if not self.valueType.isNil():
|
|
stop = self.valueType.getRelativeBoundaries().stop
|
|
if not self.value.isNil():
|
|
stop = self.value.getRelativeBoundaries().stop
|
|
if self.pragmas.len() > 0:
|
|
stop = getRelativeBoundaries(self.pragmas[^1]).stop
|
|
result = (start, stop)
|
|
of NodeKind.typeDecl:
|
|
result = (self.token.relPos.start, TypeDecl(self).name.getRelativeBoundaries().stop)
|
|
of NodeKind.importStmt:
|
|
result = (self.token.relPos.start, getRelativeBoundaries(ImportStmt(self).moduleName).stop)
|
|
of NodeKind.exprStmt:
|
|
result = getRelativeBoundaries(ExprStmt(self).expression)
|
|
of NodeKind.unaryExpr:
|
|
var self = UnaryExpr(self)
|
|
result = (self.operator.relPos.start, getRelativeBoundaries(self.a).stop)
|
|
of NodeKind.binaryExpr:
|
|
var self = BinaryExpr(self)
|
|
result = (getRelativeBoundaries(self.a).start, getRelativeBoundaries(self.b).stop)
|
|
of NodeKind.assignExpr:
|
|
var self = AssignExpr(self)
|
|
result = (getRelativeBoundaries(self.name).start, getRelativeBoundaries(self.value).stop)
|
|
of NodeKind.callExpr:
|
|
var self = CallExpr(self)
|
|
result = (getRelativeBoundaries(self.callee).start, self.closeParen.relPos.stop)
|
|
of NodeKind.getterExpr:
|
|
var self = GetterExpr(self)
|
|
result = (getRelativeBoundaries(self.obj).start, getRelativeBoundaries(self.name).stop)
|
|
of NodeKind.pragmaExpr:
|
|
var self = Pragma(self)
|
|
let start = self.token.relPos.start
|
|
var stop = 0
|
|
if self.args.len() > 0:
|
|
stop = self.args[^1].token.relPos.stop + 1
|
|
else:
|
|
stop = self.token.relPos.stop + 1
|
|
# -8 so the error highlights the #pragma[ part as well
|
|
result = (self.token.relPos.start - 8, stop)
|
|
of NodeKind.genericExpr:
|
|
var self = GenericExpr(self)
|
|
let ident = getRelativeBoundaries(self.ident)
|
|
var stop: int = ident.stop
|
|
if self.args.len() > 0:
|
|
stop = getRelativeBoundaries(self.args[^1]).stop
|
|
# Take the "]" into account
|
|
inc(stop)
|
|
result = (ident.start, stop)
|
|
of NodeKind.refExpr:
|
|
var self = Ref(self)
|
|
result = (self.token.relPos.start, self.value.getRelativeBoundaries().stop)
|
|
of NodeKind.ptrExpr:
|
|
var self = Ptr(self)
|
|
result = (self.token.relPos.start, self.value.getRelativeBoundaries().stop)
|
|
of NodeKind.constExpr:
|
|
var self = Const(self)
|
|
result = (self.token.relPos.start, self.value.getRelativeBoundaries().stop)
|
|
else:
|
|
# A good chunk of node types has enough information
|
|
# in their token object already
|
|
result = self.token.relPos
|
|
|