This commit is contained in:
Vyacheslav Matyukhin 2022-10-05 19:19:22 +04:00
parent 8cbbdf5489
commit 19a44eb12f
No known key found for this signature in database
GPG Key ID: 3D2A774C5489F96C
5 changed files with 7 additions and 5 deletions

View File

@ -197,7 +197,7 @@ describe("Peggy parse", () => {
describe("lambda", () => { describe("lambda", () => {
testParse("{|x| x}", "{{|:x| :x}}") testParse("{|x| x}", "{{|:x| :x}}")
testParse("f={|x| x}", "{:f = {{|:x| :x}}}") testParse("f={|x| x}", "{:f = {|:x| :x}}")
testParse("f(x)=x", "{:f = {|:x| {:x}}}") // Function definitions are lambda assignments testParse("f(x)=x", "{:f = {|:x| {:x}}}") // Function definitions are lambda assignments
testParse("f(x)=x ? 1 : 0", "{:f = {|:x| {(::$$_ternary_$$ :x 1 0)}}}") // Function definitions are lambda assignments testParse("f(x)=x ? 1 : 0", "{:f = {|:x| {(::$$_ternary_$$ :x 1 0)}}}") // Function definitions are lambda assignments
}) })

View File

@ -135,7 +135,7 @@ describe("Peggy to Expression", () => {
describe("lambda", () => { describe("lambda", () => {
testToExpression("{|x| x}", "{|x| x}", ~v="lambda(x=>internal code)", ()) testToExpression("{|x| x}", "{|x| x}", ~v="lambda(x=>internal code)", ())
testToExpression("f={|x| x}", "f = {{|x| x}}", ()) testToExpression("f={|x| x}", "f = {|x| x}", ())
testToExpression("f(x)=x", "f = {|x| {x}}", ()) // Function definitions are lambda assignments testToExpression("f(x)=x", "f = {|x| {x}}", ()) // Function definitions are lambda assignments
testToExpression("f(x)=x ? 1 : 0", "f = {|x| {x ? (1) : (0)}}", ()) testToExpression("f(x)=x ? 1 : 0", "f = {|x| {x ? (1) : (0)}}", ())
}) })

View File

@ -102,7 +102,7 @@ describe("stacktraces", () => {
)->toBe(`Error: There are function matches for add(), but with different arguments: [add(number, number)]; [add(distribution, number)]; [add(number, distribution)]; [add(distribution, distribution)]; [add(date, duration)]; [add(duration, duration)] )->toBe(`Error: There are function matches for add(), but with different arguments: [add(number, number)]; [add(distribution, number)]; [add(number, distribution)]; [add(distribution, distribution)]; [add(date, duration)]; [add(duration, duration)]
Stack trace: Stack trace:
f at line 4, column 5 f at line 4, column 5
<anonymous> at line 6, column 12 g at line 6, column 12
h at line 7, column 10 h at line 7, column 10
<top> at line 8, column 3 <top> at line 8, column 3
`) `)

View File

@ -7,7 +7,7 @@
start start
= _nl start:outerBlock _nl finalComment? {return start} = _nl start:outerBlock _nl finalComment? {return start}
zeroOMoreArgumentsBlockOrExpression = innerBlockOrExpression / lambda zeroOMoreArgumentsBlockOrExpression = lambda / innerBlockOrExpression
outerBlock outerBlock
= statements:array_statements finalExpression: (statementSeparator @expression)? = statements:array_statements finalExpression: (statementSeparator @expression)?

View File

@ -228,7 +228,9 @@ export function nodeLetStatement(
value: AnyPeggyNode, value: AnyPeggyNode,
location: LocationRange location: LocationRange
): NodeLetStatement { ): NodeLetStatement {
return { type: "LetStatement", variable, value, location }; const patchedValue =
value.type === "Lambda" ? { ...value, name: variable.value } : value;
return { type: "LetStatement", variable, value: patchedValue, location };
} }
export function nodeModuleIdentifier(value: string, location: LocationRange) { export function nodeModuleIdentifier(value: string, location: LocationRange) {
return { type: "ModuleIdentifier", value, location }; return { type: "ModuleIdentifier", value, location };