Initial work on type declarations
This commit is contained in:
parent
1d228c6310
commit
aed0f6e8f2
|
@ -30,7 +30,8 @@ type
|
||||||
## precedence
|
## precedence
|
||||||
|
|
||||||
# Declarations
|
# Declarations
|
||||||
funDecl = 0'u8,
|
typeDecl = 0'u8
|
||||||
|
funDecl,
|
||||||
varDecl,
|
varDecl,
|
||||||
# Statements
|
# Statements
|
||||||
forStmt, # Unused for now (for loops are compiled to while loops)
|
forStmt, # Unused for now (for loops are compiled to while loops)
|
||||||
|
@ -97,6 +98,7 @@ type
|
||||||
# work properly
|
# work properly
|
||||||
Declaration* = ref object of ASTNode
|
Declaration* = ref object of ASTNode
|
||||||
## A declaration
|
## A declaration
|
||||||
|
isPrivate*: bool
|
||||||
pragmas*: seq[Pragma]
|
pragmas*: seq[Pragma]
|
||||||
generics*: seq[tuple[name: IdentExpr, cond: Expression]]
|
generics*: seq[tuple[name: IdentExpr, cond: Expression]]
|
||||||
|
|
||||||
|
@ -245,7 +247,6 @@ type
|
||||||
name*: IdentExpr
|
name*: IdentExpr
|
||||||
value*: Expression
|
value*: Expression
|
||||||
isConst*: bool
|
isConst*: bool
|
||||||
isPrivate*: bool
|
|
||||||
isLet*: bool
|
isLet*: bool
|
||||||
valueType*: Expression
|
valueType*: Expression
|
||||||
|
|
||||||
|
@ -257,10 +258,16 @@ type
|
||||||
defaults*: seq[Expression]
|
defaults*: seq[Expression]
|
||||||
isAsync*: bool
|
isAsync*: bool
|
||||||
isGenerator*: bool
|
isGenerator*: bool
|
||||||
isPrivate*: bool
|
|
||||||
isPure*: bool
|
isPure*: bool
|
||||||
returnType*: Expression
|
returnType*: Expression
|
||||||
hasExplicitReturn*: bool
|
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
|
Pragma* = ref object of Expression
|
||||||
name*: IdentExpr
|
name*: IdentExpr
|
||||||
args*: seq[LiteralExpr]
|
args*: seq[LiteralExpr]
|
||||||
|
@ -589,6 +596,20 @@ proc newFunDecl*(name: IdentExpr, arguments: seq[tuple[name: IdentExpr, valueTyp
|
||||||
result.generics = generics
|
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 =
|
proc `$`*(self: ASTNode): string =
|
||||||
if self == nil:
|
if self == nil:
|
||||||
return "nil"
|
return "nil"
|
||||||
|
|
|
@ -1083,7 +1083,7 @@ proc parsePragma(self: Parser): Pragma =
|
||||||
## Pragmas used at the
|
## Pragmas used at the
|
||||||
## top level are either
|
## top level are either
|
||||||
## used for compile-time
|
## used for compile-time
|
||||||
## switches or for variable
|
## switches or for global variable
|
||||||
## declarations
|
## declarations
|
||||||
var decl: VarDecl
|
var decl: VarDecl
|
||||||
for node in self.tree:
|
for node in self.tree:
|
||||||
|
@ -1095,6 +1095,10 @@ proc parsePragma(self: Parser): Pragma =
|
||||||
# TODO
|
# TODO
|
||||||
|
|
||||||
|
|
||||||
|
proc typeDecl(self: Parser): TypeDecl =
|
||||||
|
## Parses type declarations
|
||||||
|
|
||||||
|
|
||||||
proc declaration(self: Parser): Declaration =
|
proc declaration(self: Parser): Declaration =
|
||||||
## Parses declarations
|
## Parses declarations
|
||||||
case self.peek().kind:
|
case self.peek().kind:
|
||||||
|
@ -1107,18 +1111,21 @@ proc declaration(self: Parser): Declaration =
|
||||||
result = self.funDecl()
|
result = self.funDecl()
|
||||||
of Coroutine:
|
of Coroutine:
|
||||||
discard self.step()
|
discard self.step()
|
||||||
result = self.funDecl(isAsync = true)
|
result = self.funDecl(isAsync=true)
|
||||||
of Generator:
|
of Generator:
|
||||||
discard self.step()
|
discard self.step()
|
||||||
result = self.funDecl(isGenerator = true)
|
result = self.funDecl(isGenerator=true)
|
||||||
of Operator:
|
of Operator:
|
||||||
discard self.step()
|
discard self.step()
|
||||||
result = self.funDecl(isOperator = true)
|
result = self.funDecl(isOperator=true)
|
||||||
of TokenType.Comment:
|
of TokenType.Comment:
|
||||||
let tok = self.step()
|
let tok = self.step()
|
||||||
if tok.lexeme.startsWith("#pragma["):
|
if tok.lexeme.startsWith("#pragma["):
|
||||||
result = self.parsePragma()
|
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
|
discard self.step() # TODO
|
||||||
else:
|
else:
|
||||||
result = Declaration(self.statement())
|
result = Declaration(self.statement())
|
||||||
|
|
Loading…
Reference in New Issue