Skip to content

Commit eb2763b

Browse files
committed
inequality operation, better input parsing and example algorithm
1 parent 3f5eb14 commit eb2763b

4 files changed

Lines changed: 97 additions & 6 deletions

File tree

examples/functions.bbee

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
const checkPrime = func (i) => {
2+
mut isPrime = 1,
3+
4+
mut j = 1,
5+
for (j = j + 1, j < i - 1 && isPrime) {
6+
if ((i % j) == 0) {
7+
isPrime = 0
8+
}
9+
},
10+
11+
isPrime
12+
},
13+
14+
const nthPrime = func (n) => (
15+
mut count = 0,
16+
mut number = 2,
17+
for (count < n) {
18+
const check = checkPrime@number,
19+
if (check) {
20+
count = count + 1
21+
},
22+
number = number + 1
23+
},
24+
25+
number - 1
26+
),
27+
28+
const nthFibonacci = func (n) => {
29+
mut a = 1,
30+
mut b = 1,
31+
32+
for (n = n - 1, n > 0) {
33+
mut c = b,
34+
b = a + b,
35+
a = c
36+
},
37+
38+
b
39+
},
40+
41+
const nthTriangular = func (n) => {
42+
n * (n + 1) / 2
43+
},
44+
45+
const nthPentagonal = func (n) => {
46+
n * (3 * n - 1) / 2
47+
},
48+
49+
output "Welcome to my program\ntype exit to quit\n",
50+
51+
mut user = 0,
52+
53+
for (user != "exit") {
54+
output "choose a sequence then n\n",
55+
output "1 - Primes\n",
56+
output "2 - Fibonacci\n",
57+
output "3 - Triangular\n",
58+
output "4 - Pentagonal\n",
59+
user = input,
60+
61+
if(user == 1) {
62+
output ("output:" + stringify (nthPrime@(input)))
63+
} else {
64+
if(user == 2) {
65+
output ("output:" + stringify (nthFibonacci@(input)))
66+
} else {
67+
if(user == 3) {
68+
output ("output:" + stringify (nthTriangular@(input)))
69+
} else {
70+
if(user == 4) {
71+
output ("output:" + stringify (nthPentagonal@(input)))
72+
} else {
73+
output "Wrong operation"
74+
}
75+
}
76+
}
77+
},
78+
79+
output "\n ------------ \n"
80+
}

language/interpreter.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,10 @@ export default function interpret(
5050
const fn: Value = interpretHere(expression.left);
5151
if (typeof fn != "string" && typeof fn != "number") {
5252
const values: Record<identifier, Variable> = {};
53-
if(fn.inputs.length != expression.inputs.length) throw new Error("number of inputs to function call does not match number in declaration")
53+
if (fn.inputs.length != expression.inputs.length)
54+
throw new Error(
55+
"number of inputs to function call does not match number in declaration"
56+
);
5457
for (let [index, identifier] of Object.entries(fn.inputs)) {
5558
values[identifier] = {
5659
value: interpretHere(expression.inputs[Number(index)]),
@@ -180,6 +183,11 @@ export default function interpret(
180183
const right = interpretHere(expression.right);
181184
return left === right ? 1 : 0;
182185
}
186+
case "!=": {
187+
const left = interpretHere(expression.left);
188+
const right = interpretHere(expression.right);
189+
return left !== right ? 1 : 0;
190+
}
183191
case "&&": {
184192
const left = interpretHere(expression.left);
185193
const right = interpretHere(expression.right);
@@ -273,9 +281,10 @@ export default function interpret(
273281
return Math.random();
274282
case "input":
275283
const input = prompt("input:") ?? "";
276-
const float = parseFloat(input);
277-
if (isNaN(float)) throw new Error("Input is not a number");
278-
return float;
284+
285+
if (/^[+-]?[0-9]+(\.[0-9]+)?$/.test(input)) return parseFloat(input);
286+
287+
return input;
279288
default:
280289
throw new Error("Unknown Macro: " + expression.identifier);
281290
}

language/lexer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ type TokenRegex = { kind: TokenKind; regex: RegExp };
5555
const TokenMap: Array<TokenRegex> = [
5656
{
5757
kind: TokenKind.unary,
58-
regex: /^((!)|(floor)|(round)|(output)|(stringify))/,
58+
regex: /^((!(?!=))|(floor)|(round)|(output)|(stringify))/,
5959
},
6060
{
6161
kind: TokenKind.additive,
@@ -79,7 +79,7 @@ const TokenMap: Array<TokenRegex> = [
7979
},
8080
{
8181
kind: TokenKind.comparative,
82-
regex: /^((>=)|(<=)|(==)|>|<)/,
82+
regex: /^((>=)|(<=)|(==)|(!=)|>|<)/,
8383
},
8484
{
8585
kind: TokenKind.logical,

language/parser.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ export type Expression =
113113

114114
export const PRECEDENCE: TokenKind[] = [
115115
// comma
116+
// declaration
116117
// assignment
117118
// function declarations
118119
// Loops
@@ -219,6 +220,7 @@ export class Parser {
219220

220221
private parseFunctionDeclaration(): Expression {
221222
if (this.top() && this.top().kind != TokenKind.func) return this.parseLoop();
223+
222224
this.pop(); // FUNC
223225
const inputs = this.parseFunctionInputs();
224226
if (this.top().kind !== TokenKind.arrow) this.pop(); // => optional

0 commit comments

Comments
 (0)