Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLI as part of Kin's Core #56

Merged
merged 4 commits into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions bin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<p align="center">
<a href="https://github.com/pacifiquem" target="blank"><img src="https://github.com/kin-lang/kin/blob/main/kin-logo.svg" width="120" alt="Kin Logo" /></a>
</p>

<p align="center">Write computer programs in Kinyarwanda! </p>

**Kin** is a straightforward programming language created with the purpose of aiding Kinyarwanda speakers in easily learning programming.

> This is CLI package for Kin Programming Language

## Installation

```shell
npm i -g @kin-lang/cli
```

## Usage

```shell
kin <command> <arguments>
```

## License

Kin's CLI is under MIT license.
71 changes: 71 additions & 0 deletions bin/kin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#!/usr/bin/env node

import { program } from 'commander';
import pkg from '../package.json';
import { readFile } from 'fs/promises';
import { Interpreter, Parser, createGlobalEnv } from '../src/index';
import * as readline from 'readline/promises';

const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});

program
.name('kin')
.description(
'Kin Programming Language: write computer programs in Kinyarwanda. @cli',
)
.usage('command [arguments]')
.version(
`\x1b[1mv${pkg.version}\x1b[0m`,
'-v, --version',
"Output kin's current version.",
)
.helpOption('-h, --help', 'Output usage of Kin.');

program
.command('repl')
.description("Enter Kin's Repl")
.action(async () => {
const parser = new Parser();
const env = createGlobalEnv(process.cwd());

console.log(`Repl ${pkg.version} (Kin)`);

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

// check for no user input or exit keyword.
if (!input || input.includes('.exit')) {
process.exit(1);
}

const program = parser.produceAST(input);

Interpreter.evaluate(program, env);
}
});

program
.command('run <file_location>')
.description('Runs a given file.')
.action(async (file_location) => {
try {
const source_codes = await readFile(file_location, 'utf-8');
const parser = new Parser();
const ast = parser.produceAST(source_codes); // Produce AST for Kin
const env = createGlobalEnv(file_location); // create global environment for Kin
Interpreter.evaluate(ast, env); // Evaluate the program
process.exit(0);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (error: any) {
if (error.code === 'ENOENT') {
console.error(`Kin Error: Can't resolve file at '${file_location}'`);
} else {
console.error(`Error reading file: ${(error as Error).message}`);
}
}
});

program.parse();
43 changes: 32 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 9 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{
"name": "@kin-lang/core",
"version": "0.4.0",
"name": "@kin-lang/kin",
"version": "0.1.0",
"description": "Kin Programming Language: write computer programs in Kinyarwanda.",
"main": "./dist/index.js",
"main": "./dist/src/index.js",
"bin": "./dist/bin/kin.js",
"author": "MURANGWA Pacifique",
"license": "Apache-2.0",
"private": false,
Expand All @@ -29,8 +30,8 @@
"interpreter"
],
"devDependencies": {
"@types/prompt-sync": "^4.2.3",
"@types/node": "^20.11.9",
"@types/prompt-sync": "^4.2.3",
"@typescript-eslint/eslint-plugin": "^6.19.1",
"@typescript-eslint/parser": "^6.19.1",
"@vitest/coverage-v8": "^1.2.2",
Expand All @@ -44,7 +45,9 @@
"vitest": "^1.2.2"
},
"dependencies": {
"prompt-sync": "^4.2.0",
"moment": "^2.30.1"
"chalk": "^5.3.0",
"commander": "^12.0.0",
"moment": "^2.30.1",
"prompt-sync": "^4.2.0"
}
}
4 changes: 0 additions & 4 deletions src/lexer/lexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,6 @@ class Lexer {
return this.makeTokenWithLexeme(TokenType.NIBA, 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 === 'niba_byanze')
return this.makeTokenWithLexeme(TokenType.NIBA_BYANZE, lexeme);
if (lexeme === 'subiramo_niba')
Expand Down
2 changes: 0 additions & 2 deletions src/lexer/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ enum TokenType {
NIBA,
NTAHINDUKA,
NANONE_NIBA,
UMUBARE,
UMUBARE_WIBICE,
NIBA_BYANZE,
SUBIRAMO_NIBA,
TANGA,
Expand Down
30 changes: 22 additions & 8 deletions src/runtime/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
* 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';
import { Interpreter } from '..';
import { Identifier, MemberExpr } from '../parser/ast';
import { NumberVal, ObjectVal, RuntimeVal, StringVal } from './values';

export default class Environment {
private parent?: Environment;
Expand Down Expand Up @@ -78,13 +79,26 @@ export default class Environment {

let pastVal = env.variables.get(varname) as ObjectVal;

const prop = property
? property.symbol
: (expr.property as Identifier).symbol;
const currentProp =
const prop = (
property
? property.symbol
: !expr.computed
? (expr.property as Identifier).symbol
: // eslint-disable-next-line @typescript-eslint/no-explicit-any
(Interpreter.evaluate(expr.property, env) as StringVal | NumberVal)
.value
).toString();

const currentProp = (
expr.property.kind == 'Identifier'
? (expr.property as Identifier).symbol
: (expr.property as NumericLiteral).value.toString();
? expr.computed
? // eslint-disable-next-line @typescript-eslint/no-explicit-any
(Interpreter.evaluate(expr.property, env) as any).value
: (expr.property as Identifier).symbol
: // eslint-disable-next-line @typescript-eslint/no-explicit-any
(Interpreter.evaluate(expr.property, env) as StringVal | NumberVal)
.value
).toString();

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

Expand Down
2 changes: 1 addition & 1 deletion src/runtime/eval/statements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export default class EvalStmt {
return MK_NULL();
}
}
public static eval_for_statement(
public static eval_loop_statement(
declaration: LoopStatement,
env: Environment,
): RuntimeVal {
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,8 @@ export function createGlobalEnv(filename: string): Environment {
.set(
'ingano',
MK_NATIVE_FN((args) => {
const keys = args[0] as ObjectVal;
return MK_NUMBER(keys.properties.size);
const obj = args[0] as ObjectVal;
return MK_NUMBER(obj.properties.size);
}),
)
.set(
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export class Interpreter {
env,
);
case 'LoopStatement':
return EvalStmt.eval_for_statement(astNode as LoopStatement, env);
return EvalStmt.eval_loop_statement(astNode as LoopStatement, env);
case 'VariableDeclaration':
return EvalStmt.eval_val_declaration(
astNode as VariableDeclaration,
Expand Down
23 changes: 12 additions & 11 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
{
"compilerOptions": {
"target": "ESNext",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"declaration": true,
"outDir": "dist",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true,
"target": "ESNext",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"declaration": true,
"outDir": "dist",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true,
"resolveJsonModule": true
},
"include": ["src/**/*"],
"include": ["src/**/*", "bin/**/*"],
"exclude": ["node_modules", "dist", "test", "coverage"]
}
}
Loading