MCPcopy Create free account
hub / github.com/TraderAlice/OpenAlice / parseProgram

Method parseProgram

src/domain/analysis/calc-v2/parser.ts:40–72  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

38 private skipNewlines(): void { while (this.at('newline')) this.next() }
39
40 parseProgram(): Program {
41 const bindings: Assign[] = []
42 let result: Expr | undefined
43
44 this.skipNewlines()
45 while (!this.at('eof')) {
46 if (result !== undefined) {
47 const t = this.peek()
48 throw new CalcError({ kind: 'syntax', message: 'Only the final line may be a bare result expression; earlier lines must be `name = ...` bindings', line: t.pos.line, col: t.pos.col })
49 }
50 // assignment vs final expression
51 if (this.at('name') && this.peek(1).type === '=') {
52 const name = this.next().value
53 const pos = this.peek().pos
54 this.eat('=')
55 bindings.push({ type: 'assign', name, value: this.parseExpr(), pos })
56 } else {
57 result = this.parseExpr()
58 }
59 if (this.at('name') && KEYWORDS.has(this.peek().value)) {
60 const t = this.peek()
61 throw new CalcError({ kind: 'reflex', message: `\`${t.value}\` is not supported — v2 is a pure expression language (no if/else/and/or/loops)`, line: t.pos.line, col: t.pos.col, suggestion: 'Compute the values you need and compare them in your own reasoning, not inside the script.' })
62 }
63 if (!this.at('eof')) this.eat('newline')
64 this.skipNewlines()
65 }
66
67 if (result === undefined) {
68 const last = this.toks[this.toks.length - 1]
69 throw new CalcError({ kind: 'syntax', message: 'Script must end with a result expression (e.g. `sma(s.close, 50)`)', line: last.pos.line })
70 }
71 return { type: 'program', bindings, result }
72 }
73
74 private parseExpr(): Expr { return this.parseAdditive() }
75

Callers 1

parseFunction · 0.80

Calls 8

skipNewlinesMethod · 0.95
atMethod · 0.95
peekMethod · 0.95
nextMethod · 0.95
eatMethod · 0.95
parseExprMethod · 0.95
pushMethod · 0.65
hasMethod · 0.65

Tested by

no test coverage detected