Initial work on type declarations

This commit is contained in:
Mattia Giambirtone 2022-06-02 14:23:05 +02:00
parent 1d228c6310
commit aed0f6e8f2
2 changed files with 36 additions and 8 deletions

View File

@ -30,7 +30,8 @@ type
## precedence
# Declarations
funDecl = 0'u8,
typeDecl = 0'u8
funDecl,
varDecl,
# Statements
forStmt, # Unused for now (for loops are compiled to while loops)
@ -97,6 +98,7 @@ type
# work properly
Declaration* = ref object of ASTNode
## A declaration
isPrivate*: bool
pragmas*: seq[Pragma]
generics*: seq[tuple[name: IdentExpr, cond: Expression]]
@ -245,7 +247,6 @@ type
name*: IdentExpr
value*: Expression
isConst*: bool
isPrivate*: bool
isLet*: bool
valueType*: Expression
@ -257,10 +258,16 @@ type
defaults*: seq[Expression]
isAsync*: bool
isGenerator*: bool
isPrivate*: bool
isPure*: bool
returnType*: Expression
hasExplicitReturn*: bool
TypeDecl* = ref object of Declaration
name*: IdentExpr
fields*: seq[tuple[name: IdentExpr, valueType: Expression,
mutable: bool, isRef: bool, isPtr: bool]]
defaults*: seq[Expression]
Pragma* = ref object of Expression
name*: IdentExpr
args*: seq[LiteralExpr]
@ -589,6 +596,20 @@ proc newFunDecl*(name: IdentExpr, arguments: seq[tuple[name: IdentExpr, valueTyp
result.generics = generics
proc newTypeDecl*(name: IdentExpr, fields: seq[tuple[name: IdentExpr, valueType: Expression, mutable: bool, isRef: bool, isPtr: bool]],
defaults: seq[Expression], isPrivate: bool, token: Token, pragmas: seq[Pragma],
generics: seq[tuple[name: IdentExpr, cond: Expression]]): TypeDecl =
result = TypeDecl(kind: typeDecl)
result.name = name
result.fields = fields
result.defaults = defaults
result.isPrivate = isPrivate
result.token = token
result.pragmas = pragmas
result.generics = generics
proc `$`*(self: ASTNode): string =
if self == nil:
return "nil"

View File

@ -1083,7 +1083,7 @@ proc parsePragma(self: Parser): Pragma =
## Pragmas used at the
## top level are either
## used for compile-time
## switches or for variable
## switches or for global variable
## declarations
var decl: VarDecl
for node in self.tree:
@ -1095,6 +1095,10 @@ proc parsePragma(self: Parser): Pragma =
# TODO
proc typeDecl(self: Parser): TypeDecl =
## Parses type declarations
proc declaration(self: Parser): Declaration =
## Parses declarations
case self.peek().kind:
@ -1107,18 +1111,21 @@ proc declaration(self: Parser): Declaration =
result = self.funDecl()
of Coroutine:
discard self.step()
result = self.funDecl(isAsync = true)
result = self.funDecl(isAsync=true)
of Generator:
discard self.step()
result = self.funDecl(isGenerator = true)
result = self.funDecl(isGenerator=true)
of Operator:
discard self.step()
result = self.funDecl(isOperator = true)
result = self.funDecl(isOperator=true)
of TokenType.Comment:
let tok = self.step()
if tok.lexeme.startsWith("#pragma["):
result = self.parsePragma()
of Type, TokenType.Whitespace, TokenType.Tab:
of Type:
discard self.step()
result = self.typeDecl()
of TokenType.Whitespace, TokenType.Tab:
discard self.step() # TODO
else:
result = Declaration(self.statement())