Skip to content

Commit 6b7dfb3

Browse files
author
manash
committed
Showed Gamakshi! <3
1 parent 4028fdc commit 6b7dfb3

File tree

17 files changed

+163
-19
lines changed

17 files changed

+163
-19
lines changed

examples/binary.gama

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
let pyramid = func(depth) {
2+
if (depth == 4) {
3+
return null;
4+
}
5+
if (depth == 1) {
6+
puts(' *')
7+
}
8+
if (depth == 2) {
9+
puts(' * *');
10+
}
11+
if (depth == 3) {
12+
puts('* *')
13+
}
14+
pyramid(depth + 1);
15+
}
16+
17+
pyramid(1);

examples/fibo.gama

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ let fibo = func(n) {
77
fibo(n - 1) + fibo(n - 2);
88
}
99
}
10-
11-
fibo(5);
10+
let x = fibo(5);
11+
puts(x);

examples/max.gama

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
let ar = [1, 5, 7, 9, 10, 25];
2+
3+
let getMax = func(arr, already) {
4+
if(isEmpty(arr)) {
5+
return already
6+
}
7+
let x = head(arr);
8+
let nex = tail(arr);
9+
if (x > already) {
10+
return getMax(nex, x);
11+
}
12+
return getMax(nex, already);
13+
}
14+
15+
puts(getMax(ar, -100000));

index.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import {readFile} from "fs";
2+
import {Parser} from "./parser/parser";
3+
import {startRepl} from "./repl";
4+
5+
const args = process.argv;
6+
7+
function sourceFile() {
8+
const path = args.at(2)!;
9+
readFile(path, (err, data) => {
10+
if (err) {
11+
console.error(`${err.message}: For file ${path}`);
12+
}
13+
try {
14+
Parser.create(data.toString()).parse().eval();
15+
} catch (err: any) {
16+
console.error(err.message);
17+
}
18+
});
19+
}
20+
function start() {
21+
if (args.length > 2) {
22+
sourceFile();
23+
return;
24+
}
25+
startRepl();
26+
}
27+
28+
start();

lexer/token.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ keywords.set("false", TOKEN.FALSE);
4444
keywords.set("true", TOKEN.TRUE);
4545
keywords.set("let", TOKEN.LET);
4646
keywords.set("func", TOKEN.FUNCTION);
47+
keywords.set("null", TOKEN.NULL);
4748

4849
export class Token {
4950
private _token: TOKEN;

package-lock.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "A Compiler For the Gamakshi Language",
55
"main": "index.js",
66
"scripts": {
7-
"start": "ts-node index.js",
7+
"start": "ts-node index.ts",
88
"test": "npx jest test",
99
"repl": "npx ts-node repl.ts"
1010
},
@@ -17,6 +17,7 @@
1717
"devDependencies": {
1818
"@types/chai": "^4.3.1",
1919
"@types/jest": "^27.5.1",
20+
"@types/node": "^18.0.0",
2021
"@typescript-eslint/eslint-plugin": "^5.29.0",
2122
"@typescript-eslint/parser": "^5.29.0",
2223
"chai": "^4.3.6",

parser/context.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
*/
1313

1414
import {HeadFn} from "./exprs/basicFunctions/head";
15+
import {IsEmptyFn} from "./exprs/basicFunctions/isEmpty";
1516
import {Length} from "./exprs/basicFunctions/len";
1617
import {PushFn} from "./exprs/basicFunctions/push";
1718
import {Puts} from "./exprs/basicFunctions/puts";
@@ -60,26 +61,29 @@ class ExecutionContext {
6061
setVariable(name: string, val: any) {
6162
const mp = this.getTop();
6263
if (mp.has(name)) {
63-
throw new Error(`Variable ${name} is already declared`);
64+
throw new Error(`Runtime Error: Variable ${name} is already declared`);
6465
}
6566
if (this.basic.has(name)) {
66-
throw new Error(`Cannot Declare builtin function: ${name}`);
67+
throw new Error(`Runtime Error: Cannot Declare builtin function: ${name}`);
6768
}
6869
mp.set(name, val);
6970
}
7071
setForce(name: string, val: any) {
7172
if (this.isBuiltin(name)) {
72-
throw new Error(`Cannot set builtin function ${name} as arguments`);
73+
throw new Error(`Runtime Error: Cannot set builtin function ${name} as arguments`);
7374
}
7475
const mp = this.getTop();
7576
mp.set(name, val);
7677
}
7778
getVariable(name: string) {
7879
const mp = this.getTop();
7980
if (!mp.has(name) && !this.basic.has(name)) {
80-
throw new Error(`Variable ${name} is not declared`);
81+
throw new Error(`Runtime Error: Variable ${name} is not declared`);
8182
}
82-
return mp.get(name) || this.basic.get(name)!;
83+
if(mp.has(name)) {
84+
return mp.get(name)!;
85+
}
86+
return this.basic.get(name)!;
8387
}
8488
constructor() {
8589
this.stack = new Stack<Map<string, any>>();
@@ -90,6 +94,7 @@ class ExecutionContext {
9094
this.basic.set("head", HeadFn.create());
9195
this.basic.set("tail", TailFn.create());
9296
this.basic.set('puts', Puts.create());
97+
this.basic.set('isEmpty', IsEmptyFn.create());
9398
}
9499
isBuiltin(str: string): boolean {
95100
return this.basic.has(str);
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import {FuncLiteral} from "../functionLiteral";
2+
3+
export class IsEmptyFn extends FuncLiteral {
4+
execute(evalArgs: any[]) {
5+
if (evalArgs.length !== 1) {
6+
throw new Error(`Runtime Error: Invalid Number of arguments in isEmpty()`);
7+
}
8+
const arg = evalArgs.at(0)!;
9+
if (arg instanceof Map) {
10+
return arg.size === 0;
11+
}
12+
const type = typeof arg;
13+
if (type === 'string' || Array.isArray(arg)) {
14+
return arg.length === 0;
15+
}
16+
throw new Error(`Runtime Error: Invaid type ${type} argument for isEmpty()`);
17+
}
18+
19+
private constructor() {
20+
super()
21+
}
22+
static create() {
23+
return new IsEmptyFn();
24+
}
25+
}

parser/exprs/functionLiteral.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import {Statement} from "../statements/statement";
1212
import {Expr, functionArgsParser} from "./expr";
1313

1414
export class FuncLiteral implements Expr {
15-
1615
args: Expr[];
1716
body: Statement; // BlockStatements
1817
env: Map<string, any> = new Map<string, any>();
@@ -32,7 +31,7 @@ export class FuncLiteral implements Expr {
3231

3332
execute(evalArgs: any[]) {
3433
if (evalArgs.length !== this.args.length) {
35-
throw new Error("Runtime Error: Incorrect number of functions arguments");
34+
throw new Error("Runtime Error: Incorrect number of functions arguments: " + this.toString());
3635
}
3736
context.pushContext(this.env);
3837
for (let i = 0; i < evalArgs.length; ++i) {

0 commit comments

Comments
 (0)