Skip to content

Commit

Permalink
Syntax: bool literals
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanjermakov committed Feb 9, 2024
1 parent f0433ae commit 82e63ff
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 10 deletions.
2 changes: 2 additions & 0 deletions nois.bnf
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ statement ::= var-def | fn-def | trait-def | impl-def | type-def | r
| CHAR
| INT
| FLOAT
| TRUE
| FALSE
| identifier
;
infix-op ::= add-op | sub-op | mult-op | div-op | exp-op | mod-op | access-op | eq-op | ne-op
Expand Down
2 changes: 1 addition & 1 deletion src/ast/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const astExprKinds = <const>[

export const astDefKinds = <const>['var-def', 'fn-def', 'trait-def', 'impl-def', 'type-def', 'field-def']

export const astLiteralKinds = <const>['string-literal', 'char-literal', 'int-literal', 'float-literal']
export const astLiteralKinds = <const>['string-literal', 'char-literal', 'int-literal', 'float-literal', 'bool-literal']

export const astPrefixOpKinds = <const>['neg-op', 'not-op', 'spread-op']

Expand Down
11 changes: 11 additions & 0 deletions src/ast/operand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export type Operand =
| CharLiteral
| IntLiteral
| FloatLiteral
| BoolLiteral
| Identifier

export const buildOperand = (node: ParseNode): Operand => {
Expand Down Expand Up @@ -50,6 +51,8 @@ export const buildOperand = (node: ParseNode): Operand => {
return buildIntLiteral(n)
case 'float':
return buildFloatLiteral(n)
case 'bool':
return buildBoolLiteral(n)
case 'identifier':
return buildIdentifier(n)
}
Expand Down Expand Up @@ -198,6 +201,14 @@ export const buildFloatLiteral = (node: ParseNode): FloatLiteral => {
return { kind: 'float-literal', parseNode: node, value: (<LexerToken>node).value }
}

export interface BoolLiteral extends AstNode<'bool-literal'>, Partial<Typed> {
value: string
}

export const buildBoolLiteral = (node: ParseNode): BoolLiteral => {
return { kind: 'bool-literal', parseNode: node, value: (<LexerToken>node).value }
}

export interface Identifier extends AstNode<'identifier'>, Partial<Typed> {
scope: Name[]
name: Name
Expand Down
16 changes: 15 additions & 1 deletion src/lexer/lexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export const lexerKeywordKinds = <const>[
'pub-keyword'
]

export const lexerDynamicKinds = <const>['name', 'string', 'char', 'int', 'float']
export const lexerDynamicKinds = <const>['name', 'string', 'char', 'int', 'float', 'bool']

const lexerParseIndependentKinds = <const>['comment']

Expand Down Expand Up @@ -117,6 +117,7 @@ export const lexerKeywordMap: Map<TokenKind, string> = new Map([
['at', '@']
])

const boolRegex = /^(true|false)/
const floatRegex = /^((\d+(\.\d*)?e[+-]?\d+)|(\d+\.\d*)|(\d*\.\d+))/
const escapeCharReg = /(\\[btnvfr\\'"])/
const unicodeCharReg = /(\\u{[0-9a-fA-F]{1,4}})/
Expand Down Expand Up @@ -159,6 +160,7 @@ export const tokenize = (code: string): LexerToken[] => {
}

const fns = [
parseBool,
parseFloat,
parseInt,
parseComment,
Expand Down Expand Up @@ -247,6 +249,18 @@ const parseName = (chars: string[], tokens: LexerToken[], pos: { pos: number }):
return false
}

const parseBool = (chars: string[], tokens: LexerToken[], pos: { pos: number }): boolean => {
const leftCode = chars.slice(pos.pos).join('')
const match = leftCode.match(boolRegex)
if (!match) return false

const bool = match[0]
const start = pos.pos
pos.pos += bool.length
tokens.push(createToken('bool', bool, pos, start))
return true
}

const parseFloat = (chars: string[], tokens: LexerToken[], pos: { pos: number }): boolean => {
if (!isNumeric(chars[pos.pos]) && chars[pos.pos] !== '.') return false
const leftCode = chars.slice(pos.pos).join('')
Expand Down
4 changes: 2 additions & 2 deletions src/parser/fns/expr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ export const parseSubExpr = (parser: Parser): void => {

/**
* operand ::= if-expr | match-expr | closure-expr | O-PAREN expr C-PAREN | list-expr | STRING | CHAR | INT | FLOAT
* | identifier | type
* TRUE | FALSE | identifier | type
*/
export const parseOperand = (parser: Parser): void => {
const dynamicTokens: TokenKind[] = ['string', 'char', 'int', 'float']
const dynamicTokens: TokenKind[] = ['string', 'char', 'int', 'float', 'bool']

const mark = parser.open()
if (parser.at('if-keyword') && parser.nth(1) === 'let-keyword') {
Expand Down
1 change: 1 addition & 0 deletions src/parser/fns/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export const exprFirstTokens: TokenKind[] = [
'match-keyword',
'int',
'float',
'bool',
'string',
'o-paren',
'o-bracket',
Expand Down
7 changes: 7 additions & 0 deletions src/semantic/expr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,13 @@ export const checkOperand = (operand: Operand, ctx: Context): void => {
typeArgs: []
}
break
case 'bool-literal':
operand.type = {
kind: 'vid-type',
identifier: vidFromString('std::bool::Bool'),
typeArgs: []
}
break
case 'identifier':
checkIdentifier(operand, ctx)
if (operand.type!.kind === 'unknown-type') {
Expand Down
4 changes: 0 additions & 4 deletions src/std/bool.no
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ pub type Bool {
False
}

pub let true: Bool = Bool::True()

pub let false: Bool = Bool::False()

impl Bool {
pub fn and(self, other: Self): Self {
andBool(self, other)
Expand Down
3 changes: 1 addition & 2 deletions src/std/prelude.no
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ pub use std::{
int::Int,
string::String,
char::Char,
// TODO: bool literals
bool::{Bool, true, false},
bool::Bool,
eq::Eq,
ord::{Ordering, Ord},
iter::{Iterable, Iter, Collector},
Expand Down

0 comments on commit 82e63ff

Please sign in to comment.