Skip to content

Commit a589cd0

Browse files
committed
Drastic changes again
1 parent 9a71c3e commit a589cd0

File tree

8 files changed

+57
-50
lines changed

8 files changed

+57
-50
lines changed

examples/factorial.funk

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
`5..1` t1t
22

3-
factorial(x) = ? x > 1 => x * factorial(x).step() : 1
3+
factorial(x) = ? x > 1 => x * factorial(x - 1) : 1
44

5-
print(factorial(x):1)
5+
factorial(x).step(5)
6+
print(x:5)

examples/fizzbuzz copy.funk

Lines changed: 0 additions & 10 deletions
This file was deleted.

examples/give_me_input.funk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ G(x) = input("Give me a value and I will multiply that sequence with that value.
66

77
h(x) = f(x) * G(x)
88
h(x).step(2)
9-
print(f(x):5)
9+
print(x:5)

examples/slices.funk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
f(x) = x ; x + 1 ; x + 2
44
f(x).step(2)
5-
print(f(x):7)
5+
print(x:7)

src/analyzer.js

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ class Context {
2525
}
2626

2727
newChildContext(props) {
28-
return new Context({ ...this, ...props, parent: this, locals: new Map() });
28+
29+
return new Context({ ...this, ...props, parent: this, locals: this.locals });
2930
}
3031
}
3132

@@ -86,9 +87,9 @@ export default function analyze(match) {
8687

8788
FuncDef(
8889
id,
89-
open,
90+
_open,
9091
param,
91-
close,
92+
_close,
9293
_eq,
9394
_newLine,
9495
body,
@@ -99,16 +100,17 @@ export default function analyze(match) {
99100
// Temporary check inside the function.
100101
const originalContext = context;
101102
context = context.newChildContext();
103+
context.add(id.sourceString, {
104+
kind: "Function",
105+
type: core.functionType,
106+
name: id.sourceString,
107+
});
102108
context.add(param.sourceString, {
103109
kind: "MutableRange",
104110
type: core.numberType,
105111
name: param.sourceString,
106112
});
107-
const functionId =
108-
id.sourceString +
109-
open.sourceString +
110-
param.sourceString +
111-
close.sourceString;
113+
const functionId = `${id.sourceString}(${param.sourceString})`;
112114
context.add(functionId, {
113115
kind: "Function",
114116
type: core.functionType,
@@ -117,7 +119,7 @@ export default function analyze(match) {
117119
const analyzedBody = body?.rep();
118120
// Checks outside of the function definition.
119121
context = originalContext;
120-
context.add(functionId, {
122+
context.add(id.sourceString, {
121123
kind: "Function",
122124
type: core.functionType,
123125
name: functionId,
@@ -136,20 +138,15 @@ export default function analyze(match) {
136138
return func;
137139
},
138140

139-
FuncCall(id, open, arg, close) {
140-
const functionId =
141-
id.sourceString +
142-
open.sourceString +
143-
arg.sourceString +
144-
close.sourceString;
145-
const func = context.lookup(functionId);
141+
FuncCall(id, _open, arg, _close) {
142+
const func = context.lookup(id.sourceString);
146143
mustHaveBeenFound(func, id.sourceString, { at: id });
147-
return core.funcCall(id.sourceString, arg.sourceString);
144+
return core.funcCall(id.sourceString, arg.rep());
148145
},
149146

150-
FunctionGroup(_open, expr, _close) {
151-
return core.functionGroup(expr.rep());
152-
},
147+
// FunctionGroup(_open, expr, _close) {
148+
// return core.functionGroup(expr.rep());
149+
// },
153150

154151
Expr(condExpr, _sep, _newLine, rest) {
155152
return core.expr(
@@ -239,7 +236,7 @@ export default function analyze(match) {
239236

240237
MulExpr_mul(left, right) {
241238
const l = left.rep();
242-
const r = right.sourceString;
239+
const r = right.rep();
243240
return core.mulExpr(l, "*", r);
244241
},
245242

@@ -283,10 +280,10 @@ export default function analyze(match) {
283280
return core.printStmt(expr.rep());
284281
},
285282

286-
StepCall(expr, _dot, _step, _open, stepValue, _close) {
283+
StepCall(id, _open, arg, _close, _dot, _step, _open1, stepValue, _close1) {
287284
// Syntax Sugar: Default step count is 1.
288285
return core.stepCall(
289-
expr.rep(),
286+
{name: id.sourceString, arg: arg.sourceString},
290287
stepValue.rep().length ? stepValue.rep()[0].value : 1
291288
);
292289
},
@@ -325,8 +322,8 @@ export default function analyze(match) {
325322
num(sign, value, _period, decimal) {
326323
const number = Number(
327324
sign.sourceString +
328-
value.sourceString +
329-
decimal.sourceString
325+
value.sourceString +
326+
decimal.sourceString
330327
);
331328
return core.num(number);
332329
},
@@ -343,7 +340,7 @@ export default function analyze(match) {
343340
const idName = firstChar.sourceString + name?.sourceString;
344341
const entity = context.lookup(idName);
345342
mustHaveBeenFound(entity, idName, { at: name });
346-
return core.id(firstChar.sourceString + name?.sourceString);
343+
return core.id(idName);
347344
},
348345

349346
_terminal(...children) {

src/core.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ export function funcCall(name, arg) {
1717
return { type: anyType, kind: "FuncCall", name, arg };
1818
}
1919

20-
export function functionGroup(expr) {
21-
return { type: expr.type, kind: "FunctionGroup", expr };
22-
}
20+
// export function functionGroup(expr) {
21+
// return { type: expr.type, kind: "FunctionGroup", expr };
22+
// }
2323

2424
export function expr(condExpr, rest = []) {
2525
return { type: condExpr.type, kind: "Expr", condExpr, rest };

src/funktion.ohm

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Funktion {
22
Program = (GlobalRange "\n")? "\n"* ListOf<(Statement | FuncDef | ""), ("\n"+ | space+)>
33

44
FuncDef = id "(" id ")" "=" "\n"? Expr (";" (FuncDef | Expr))*
5-
FuncCall = id "(" id ")"
5+
FuncCall = id "(" Expr ")"
66
Expr = CondExpr ("," "\n"? CondExpr)*
77
CondExpr = "?" CondExpr ("==" | "!=" | "<=" | "<" | ">=" | ">") CondExpr "=>" BitwiseExpr ":" "\n"? CondExpr --ternary
88
| BitwiseExpr
@@ -33,7 +33,8 @@ Funktion {
3333
| InputStmt
3434
PrintStmt = "print" "(" (TimeCall | CondExpr) ")"
3535
TimeCall = id ":" num
36-
StepCall = (FuncCall | FunctionGroup) "." "step" "(" num? ")"
36+
StepCall = id "(" id ")" "." "step" "(" num? ")"
37+
StepCallMulti = FunctionGroup "." "step" "(" num? ")"
3738
InputStmt = "input" "(" stringliteral? ")"
3839

3940
GlobalRange = range timestep?

src/generator.js

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { voidType, numberType, stringType, functionType, standardLibrary } from "./core.js";
22

33
export default function generate(program) {
4+
const inputCode = [];
5+
let inputIndex = 0;
46
const output = [];
57
const targetName = ((mapping) => {
68
return (name) => {
@@ -13,9 +15,8 @@ export default function generate(program) {
1315

1416
const gen = node => generators?.[node?.kind]?.(node) ?? node
1517

16-
const instantiatedMutableRanges = []
18+
const instantiatedMutableRanges = new Set([]);
1719

18-
// let currentFunction = { name: null, param: null };
1920

2021
const generators = {
2122
Program(p) {
@@ -28,6 +29,11 @@ export default function generate(program) {
2829
const end = range ? gen(range.end[0]) : 5;
2930
const step = timestep ? gen(timestep.value) :
3031
start <= end ? 1 : -1;
32+
inputCode.push(`
33+
import { createInterface } from "node:readline/promises";
34+
import { stdin as input, stdout as output } from "node:process";
35+
const rl = createInterface({ input, output });
36+
`);
3137
output.push(`
3238
function generateRange(start = ${start}, end = ${end}, step = ${step}) {
3339
if (end < start) step *= -1;
@@ -87,16 +93,20 @@ export default function generate(program) {
8793
}
8894
`);
8995
p.statements.forEach(gen);
96+
output.push(`rl.close()`);
97+
output.unshift(...inputCode);
9098
},
9199

92100
FuncDef(d) {
101+
console.log(d)
93102
const funcName = targetName(d.name);
94103
const param = targetName(d.param);
95104
const body = gen(d.body);
96105
const lastStmt = body.pop();
97106
output.push(`function ${funcName}(${param}) {\n${body}\nreturn ${lastStmt};\n} `);
98-
if (instantiatedMutableRanges.indexOf(param) === -1) {
107+
if (!instantiatedMutableRanges.has(param)) {
99108
output.push(`let ${param} = initializeMutableRange();`);
109+
instantiatedMutableRanges.add(param)
100110
}
101111
},
102112

@@ -105,7 +115,7 @@ export default function generate(program) {
105115
},
106116

107117
StepCall(s) {
108-
const expr = gen(s.expr);
118+
const expr = targetName(s.expr.name);
109119
const stepValue = gen(s.stepValue);
110120
const arg = targetName(s.expr.arg);
111121
output.push(`applyFunction(${arg}, ${stepValue}, ${expr});`);
@@ -188,6 +198,14 @@ export default function generate(program) {
188198
return `${gen(e.id)}.values.slice(0, ${gen(e.timeValue)})`;
189199
},
190200

201+
InputStmt(e) {
202+
inputCode.push(`
203+
console.log(${gen(e.prompt[0])});
204+
const inputVar__${inputIndex} = await rl.question("Input: ");
205+
`);
206+
return `inputVar__${inputIndex++}`
207+
},
208+
191209
num(n) {
192210
return n.value;
193211
},
@@ -205,7 +223,7 @@ export default function generate(program) {
205223
},
206224

207225
FuncCall(c) {
208-
return `${targetName(c.name)}`;
226+
return `${targetName(c.name)}(${gen(c.arg)})`;
209227
}
210228
};
211229

0 commit comments

Comments
 (0)