Skip to content

Commit

Permalink
Merge pull request #33 from kin-lang/parser
Browse files Browse the repository at this point in the history
feat: producing kin's ast
  • Loading branch information
pacifiquem authored Feb 2, 2024
2 parents fc989f8 + f265de8 commit 50444f8
Show file tree
Hide file tree
Showing 11 changed files with 352 additions and 150 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,7 @@ coverage/
# npm
*.npm-debug.log
npm-debug.log


# Custom
local.todo.md
10 changes: 5 additions & 5 deletions examples/factorial.kin
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@

porogaramu_ntoya factorial(nbr) {
niba (nbr == 1) {
tanga 1;
tanga 1
}
tanga nbr * factorial(nbr - 1);
tanga nbr * factorial(nbr - 1)
}

reka input_nbr = injiza_amakuru("Enter a number: ");
reka nbr_factorial = factorial(nbr);
tangaza_amakuru(nbr_factorial);
reka input_nbr = injiza_amakuru("Enter a number: ")
reka nbr_factorial = factorial(nbr)
tangaza_amakuru(nbr_factorial)
10 changes: 5 additions & 5 deletions examples/fibonacci.kin
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@

porogaramu_ntoya fibonacci(nbr) {
niba (nbr == 1 || nbr == 0) {
tanga 1;
tanga 1
}
tanga fibonacci(nbr - 1) + fibonacci(nbr - 2);
tanga fibonacci(nbr - 1) + fibonacci(nbr - 2)
}

reka input_nbr = injiza_amakuru("Enter a number: ");
reka nbr_fibonacci = fibonacci(nbr);
tangaza_amakuru(nbr_fibonacci);
reka input_nbr = injiza_amakuru("Enter a number: ")
reka nbr_fibonacci = fibonacci(nbr)
tangaza_amakuru(nbr_fibonacci)
14 changes: 7 additions & 7 deletions examples/fizzbuzz.kin
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@
#


reka limit = injiza_amakuru("Enter the limit (must be a number): ");
reka limit = injiza_amakuru("Enter the limit (must be a number): ")

reka response;
reka i = 1;
reka i = 1

subiramo_niba(i <= limit) {
niba (i%5 == 0 && i%3 == 0) {
response = "FizzBuzz";
response = "FizzBuzz"
} nanone_niba (i%5 == 0) {
response = "Fizz";
response = "Fizz"
} nanone_niba (i%3 == 0) {
response = "Buzz";
response = "Buzz"
} niba_byanze {
response = i;
response = i
}

tangaza_amakuru(response);
tangaza_amakuru(response)
}
32 changes: 2 additions & 30 deletions src/lexer/lexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,54 +139,26 @@ class Lexer {
const lexeme: string = this.sourceCodes.slice(start, this.currentPos);

/* Check if lexeme is a keywork */
if (lexeme === 'ubusa')
return this.makeTokenWithLexeme(TokenType.UBUSA, lexeme);
if (lexeme === 'niba')
return this.makeTokenWithLexeme(TokenType.NIBA, lexeme);
if (lexeme === 'nibyo')
return this.makeTokenWithLexeme(TokenType.NIBYO, lexeme);
if (lexeme === 'sibyo')
return this.makeTokenWithLexeme(TokenType.SIBYO, lexeme);
if (lexeme === 'nanone_niba')
return this.makeTokenWithLexeme(TokenType.NANONE_NIBA, lexeme);
if (lexeme === 'umubare')
return this.makeTokenWithLexeme(TokenType.UMUBARE, lexeme);
if (lexeme === 'umubare_wibice')
return this.makeTokenWithLexeme(TokenType.UMUBARE_WIBICE, lexeme);
if (lexeme === 'ijambo')
return this.makeTokenWithLexeme(TokenType.IJAMBO, lexeme);
if (lexeme === 'niba_byanze')
return this.makeTokenWithLexeme(TokenType.NIBA_BYANZE, lexeme);
if (lexeme === 'subiramo_NIBA')
if (lexeme === 'subiramo_niba')
return this.makeTokenWithLexeme(TokenType.SUBIRAMO_NIBA, lexeme);
if (lexeme === 'tanga')
return this.makeTokenWithLexeme(TokenType.TANGA, lexeme);
if (lexeme === 'porogaramu_ntoya')
return this.makeTokenWithLexeme(TokenType.POROGARAMU_NTOYA, lexeme);
if (lexeme === 'tangaza_amakuru')
return this.makeTokenWithLexeme(TokenType.TANGAZA_AMAKURU, lexeme);
if (lexeme === 'injiza_amakuru')
return this.makeTokenWithLexeme(TokenType.INJIZA_AMAKURU, lexeme);
if (lexeme === 'komeza')
return this.makeTokenWithLexeme(TokenType.KOMEZA, lexeme);
if (lexeme === 'hagarara')
return this.makeTokenWithLexeme(TokenType.HAGARARA, lexeme);
if (lexeme === 'ubwoko')
return this.makeTokenWithLexeme(TokenType.UBWOKO, lexeme);
if (lexeme === 'reka')
return this.makeTokenWithLexeme(TokenType.REKA, lexeme);
if (lexeme === 'ntahinduka')
return this.makeTokenWithLexeme(TokenType.NTAHINDUKA, lexeme);
if (lexeme === 'soma_inyandiko')
return this.makeTokenWithLexeme(TokenType.SOMA_INYANDIKO, lexeme);
if (lexeme === 'andika_inyandiko')
return this.makeTokenWithLexeme(TokenType.ANDIKA_INYANDIKO, lexeme);
if (lexeme === 'vugurura_inyandiko')
return this.makeTokenWithLexeme(TokenType.KUVUGURURA_INYANDIKO, lexeme);
if (lexeme === 'kin_hagarara')
return this.makeTokenWithLexeme(TokenType.KIN_HAGARARA, lexeme);
if (lexeme === 'sisitemu')
return this.makeTokenWithLexeme(TokenType.SISITEMU, lexeme);

/* Not a keywork, it's an identifier */
return this.makeTokenWithLexeme(TokenType.IDENTIFIER, lexeme);
Expand Down Expand Up @@ -260,7 +232,7 @@ class Lexer {
}
case ';':
this.advance();
return this.makeTokenWithLexeme(TokenType.END_OF_LINE, ';');
return this.makeTokenWithLexeme(TokenType.SEMI_COLON, ';');
case ']':
this.advance();
return this.makeTokenWithLexeme(TokenType.CLOSE_BRACKET, ']');
Expand Down
18 changes: 2 additions & 16 deletions src/lexer/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

enum TokenType {
/* One-character tokens */
DOT,
MINUS,
PLUS,
STAR,
Expand All @@ -13,7 +14,7 @@ enum TokenType {
MODULO,
AMPERSAND,
NEGATION,
END_OF_LINE,
SEMI_COLON,
OPEN_PARANTHESES,
CLOSE_PARANTHESES,
OPEN_BRACKET,
Expand Down Expand Up @@ -45,10 +46,7 @@ enum TokenType {
LESS_THAN_OR_EQUAL,

/* Keywords */
UBUSA,
NIBA,
NIBYO,
SIBYO,
NTAHINDUKA,
NANONE_NIBA,
UMUBARE,
Expand All @@ -57,19 +55,7 @@ enum TokenType {
SUBIRAMO_NIBA,
TANGA,
POROGARAMU_NTOYA,
TANGAZA_AMAKURU,
INJIZA_AMAKURU,
KOMEZA,
HAGARARA,
UBWOKO,
ERROR,
KIN_HAGARARA,
REKA,
SOMA_INYANDIKO,
ANDIKA_INYANDIKO,
KUVUGURURA_INYANDIKO,
SISITEMU,
IJAMBO,
EOF,
}

Expand Down
54 changes: 0 additions & 54 deletions src/main.ts

This file was deleted.

16 changes: 15 additions & 1 deletion src/parser/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export type NodeType =
| 'MemberExpression'
| 'CallExpression'
| 'BinaryExpr'
| 'ReturnExpr'

// Literals
| 'ObjectLiteral'
Expand Down Expand Up @@ -46,6 +47,14 @@ export interface Program extends Stmt {
body: Stmt[];
}

/**
* Defines a return statement
*/
export interface ReturnExpr extends Expr {
kind: 'ReturnExpr';
value?: Expr;
}

/**
* Defines a variable declaration
*/
Expand Down Expand Up @@ -87,6 +96,11 @@ export interface FunctionDeclaration extends Stmt {
body: Stmt[];
}

export interface ReturnExpr extends Expr {
kind: 'ReturnExpr';
value?: Expr;
}

/**
* Defines a binary expression
*/
Expand All @@ -100,7 +114,7 @@ export interface BinaryExpr extends Expr {
// foo["bar"]() should work
export interface CallExpr extends Expr {
kind: 'CallExpression';
callee: Expr;
caller: Expr;
args: Expr[];
}

Expand Down
Loading

0 comments on commit 50444f8

Please sign in to comment.