Skip to content

Commit

Permalink
Merge pull request #37 from kin-lang/runtime
Browse files Browse the repository at this point in the history
Chore: Kin's AST evaluation by our interpreter
  • Loading branch information
pacifiquem authored Feb 8, 2024
2 parents 871a338 + ccee170 commit b5ce9be
Show file tree
Hide file tree
Showing 12 changed files with 373 additions and 109 deletions.
15 changes: 0 additions & 15 deletions examples/factorial.kin

This file was deleted.

16 changes: 0 additions & 16 deletions examples/fibonacci.kin

This file was deleted.

2 changes: 2 additions & 0 deletions examples/fizzbuzz.kin
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,7 @@ subiramo_niba(i <= limit) {
response = i
}

i = i + 1

tangaza_amakuru(response)
}
40 changes: 9 additions & 31 deletions src/kin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,54 +6,32 @@
**************************************************************************/

import Parser from './parser/parser';
import { LogError, LogMessage } from './lib/log';
import { LogError } from './lib/log';

import * as readline from 'readline/promises';
import { readFileSync } from 'fs';

const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
import { Interpreter } from './runtime/interpreter';
import { createGlobalEnv } from './runtime/globals';

const file = process.argv[2];

if (file) {
run(file);
} else {
repl();
LogError('No file provided');
process.exit(1);
}

async function run(filename: string): Promise<void> {
try {
const input: string = readFileSync(filename, 'utf-8');
const parser = new Parser();
LogMessage(parser.produceAST(input));
const env = createGlobalEnv(); // global environment
const program = parser.produceAST(input);
Interpreter.evaluate(program, env);
process.exit(0);
} catch (error) {
const err = error as Error;
LogError(err);
process.exit(1);
}
}

async function repl() {
LogMessage('Kin Repl v0.0');

while (true) {
const input = await rl.question('> ');

// check for no user input or exit keyword.
if (input === '.exit' || input === '.quit' || input === '.q') {
process.exit(0);
}

try {
const parser = new Parser();
LogMessage(parser.produceAST(input));
} catch (error: unknown) {
const err = error as Error;
LogError(err);
process.exit(1);
}
}
}
7 changes: 2 additions & 5 deletions src/parser/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ export default class Parser {

return identifier_expr;
case TokenType.INTEGER:
case TokenType.FLOAT:
const nbr_literal: Expr = {
kind: 'NumericLiteral',
value: parseFloat(this.eat().lexeme),
Expand All @@ -188,10 +189,6 @@ export default class Parser {
); // closing paren

return value;
case TokenType.TANGA:
LogError(
`On line ${this.at().line} : Kin Error : porogaramu_ntoya niyo yonyine ishobora gutanga ikintu`,
);
default:
LogError(
`On line ${this.at().line}: Kin Error: Unexpected token ${this.at().lexeme}`,
Expand Down Expand Up @@ -308,7 +305,7 @@ export default class Parser {
private parse_multiplicative_expr(): Expr {
let left = this.parse_call_member_expr();

while (['/', '*', '%'].includes(this.at().lexeme)) {
while (['/', '*', '%', '^'].includes(this.at().lexeme)) {
const operator = this.eat().lexeme;
const right = this.parse_call_member_expr();
left = {
Expand Down
13 changes: 11 additions & 2 deletions src/runtime/environment.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { Identifier, MemberExpr } from '../parser/ast';
/*******************************************************************************************
* Kin's Environment *
* In which environment are we in? variables accessible - *
* in current scope and other questions like this are solved by Kin's Environment *
*******************************************************************************************/

import { Identifier, MemberExpr, NumericLiteral } from '../parser/ast';
import { ObjectVal, RuntimeVal } from './values';

export default class Environment {
Expand Down Expand Up @@ -61,7 +67,10 @@ export default class Environment {
const prop = property
? property.symbol
: (expr.property as Identifier).symbol;
const currentProp = (expr.property as Identifier).symbol;
const currentProp =
expr.property.kind == 'Identifier'
? (expr.property as Identifier).symbol
: (expr.property as NumericLiteral).value.toString();

if (value) pastVal.properties.set(prop, value);

Expand Down
Loading

0 comments on commit b5ce9be

Please sign in to comment.