Skip to content

Commit

Permalink
add: NullLiteral
Browse files Browse the repository at this point in the history
  • Loading branch information
thutasann committed May 22, 2024
1 parent 0abd0d5 commit 1a18403
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
8 changes: 7 additions & 1 deletion src/ast/compiler/ast.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* - statements will not return a value
* - statement example -> let x = 45
*/
export type NodeType = 'Program' | 'NumericLiteral' | 'Identifier' | 'BinaryExpr'
export type NodeType = 'Program' | 'NumericLiteral' | 'NullLiteral' | 'Identifier' | 'BinaryExpr'

/** Abstract Statement Interface that include statement kind */
export interface IStatement {
Expand Down Expand Up @@ -51,3 +51,9 @@ export interface INumericLiteral extends IExpression {
kind: 'NumericLiteral'
value: number
}

/** Null Literal Expression Node Type */
export interface INullLiteral extends IExpression {
kind: 'NullLiteral'
value: 'null'
}
6 changes: 5 additions & 1 deletion src/ast/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
IBinaryExpression,
INumericLiteral,
IIdentifierExpression,
INullLiteral,
} from './ast.interface'

/**
Expand Down Expand Up @@ -104,14 +105,17 @@ export class Parser {
switch (tokenType) {
case TokenType.Identifier:
return { kind: 'Identifier', symbol: this.eat().value } as IIdentifierExpression
case TokenType.Null:
this.eat() // advance past null keyword
return { kind: 'NullLiteral', value: 'null' } as INullLiteral
case TokenType.Number:
return { kind: 'NumericLiteral', value: parseFloat(this.eat().value) } as INumericLiteral
case TokenType.OpenParen:
this.eat() // eat open paren
const value = this.parseExpr()
this.expect(
TokenType.CloseParen,
'Unexpected token found inside parenthesised expression. Expected closing parenthesis',
'Unexpected token found inside parenthesised expression. Expected closing parenthesis'
) // eat close paren
return value
default:
Expand Down
4 changes: 2 additions & 2 deletions src/lexer/lexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ export class Lexer {
const reserved = KEYWORDS[ident]
// if value is not undefined, the identifier is recognized keyword
if (typeof reserved == 'number') {
tokens.push(this.token(ident, TokenType.Identifier))
} else {
tokens.push(this.token(ident, reserved))
} else {
tokens.push(this.token(ident, TokenType.Identifier))
}
} else if (this.isSkippable(src[0])) {
src.shift() // skip the current character
Expand Down

0 comments on commit 1a18403

Please sign in to comment.