Skip to content

Commit d0e5b41

Browse files
committed
added first draft implementation for slices for ohm grammar, core, analyzer and generator.
1 parent 62e913b commit d0e5b41

File tree

4 files changed

+42
-6
lines changed

4 files changed

+42
-6
lines changed

src/analyzer.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,14 @@ export default function analyze(match) {
151151
);
152152
},
153153

154+
SliceExpr(expr, _backslash, rest) {
155+
const expressions = [expr.rep()];
156+
if (rest.children.length > 0) {
157+
expressions.push(...rest.children.map(child => child.rep()));
158+
}
159+
return core.sliceExpr(expressions);
160+
},
161+
154162
CondExpr_ternary(
155163
_question,
156164
condLeft,

src/core.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ export function expr(condExpr, rest = []) {
2121
return { type: condExpr.type, kind: "Expr", condExpr, rest };
2222
}
2323

24+
export function sliceExpr(expressions) {
25+
return { type: expressions[0].type, kind: "SliceExpr", expressions };
26+
}
27+
2428
export function condExpr(leftCond, op, rightCond, thenBranch, elseBranch) {
2529
return {
2630
type: thenBranch?.type ?? anyType,

src/funktion.ohm

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

4-
FuncDef = id "(" id ")" "=" "\n"? Expr (";" (FuncDef | Expr))*
4+
FuncDef = id "(" id ")" "=" "\n"? SliceExpr (";" (FuncDef | SliceExpr))*
55
FuncCall = id "(" Expr ")"
66
Expr = CondExpr ("," "\n"? CondExpr)*
7+
SliceExpr = Expr ("\\" Expr)*
78
CondExpr = "?" CondExpr ("==" | "!=" | "<=" | "<" | ">=" | ">") CondExpr "=>" BitwiseExpr ":" "\n"? CondExpr --ternary
89
| BitwiseExpr
910
BitwiseExpr = BitwiseExpr ("&" | "|" | "^") ShiftExpr --binary

src/generator.js

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,22 +70,25 @@ export default function generate(program) {
7070
if (gen.size === 0) {
7171
gen.size++;
7272
gen.index++;
73-
gen.values.push(f(currentVal));
73+
const result = f(currentVal);
74+
gen.values.push(Array.isArray(result) ? result.join(' ') : result);
7475
currentVal += gen.timestepRange.step;
7576
}
7677
if (gen.timestepRange.step > 0) {
7778
while (currentVal <= gen.timestepRange.end && iterations > 0) {
7879
gen.size++;
7980
gen.index++;
80-
gen.values.push(f(currentVal));
81+
const result = f(currentVal);
82+
gen.values.push(Array.isArray(result) ? result.join(' ') : result);
8183
currentVal += gen.timestepRange.step;
8284
iterations--;
8385
}
8486
} else {
8587
while (currentVal >= gen.timestepRange.end && iterations > 0) {
8688
gen.size++;
8789
gen.index++;
88-
gen.values.push(f(currentVal));
90+
const result = f(currentVal);
91+
gen.values.push(Array.isArray(result) ? result.join(' ') : result);
8992
currentVal += gen.timestepRange.step;
9093
iterations--;
9194
}
@@ -102,8 +105,21 @@ export default function generate(program) {
102105
const funcName = targetName(d.name);
103106
const param = targetName(d.param);
104107
const body = gen(d.body);
105-
const lastStmt = body.pop();
106-
output.push(`function ${funcName}(${param}) {\n${body}\nreturn ${lastStmt};\n} `);
108+
109+
// check for slices in the body of the function
110+
if (d.body.kind === "SliceExpr") {
111+
const sliceExpressions = d.body.expressions.map(expr => {
112+
return `${gen(expr)}`;
113+
}).join(", ");
114+
115+
output.push(`function ${funcName}(${param}) {
116+
return [${sliceExpressions}];
117+
}`);
118+
} else {
119+
// handle same as before if not a slice
120+
const lastStmt = body.pop();
121+
output.push(`function ${funcName}(${param}) {\n${body}\nreturn ${lastStmt};\n} `);
122+
}
107123
if (!instantiatedMutableRanges.has(param)) {
108124
output.push(`let ${param} = initializeMutableRange();`);
109125
instantiatedMutableRanges.add(param)
@@ -129,6 +145,13 @@ export default function generate(program) {
129145
return exprs;
130146
},
131147

148+
//anywhere you see SliceExpr in the code, I added it in ~MS
149+
SliceExpr(e) {
150+
const slices = e.expressions.map(gen);
151+
// join slices with spaces in output
152+
return `[${slices.join(', ')}]`;
153+
},
154+
132155
CondExpr(e) {
133156
if (e.thenBranch) {
134157
const condleft = gen(e.leftCond);

0 commit comments

Comments
 (0)