-
-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fixed using function as argument in lox interpreter #5
Open
tkueeu
wants to merge
5
commits into
TypeFox:main
Choose a base branch
from
tkueeu:fix-function-as-value
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d385b32
fixed using function as argument in lox interpreter
tkueeu 1a37376
get rid of the unneccessary use of ternary condition operator
tkueeu 226ded9
first unit tests
tkueeu 9ab48d6
reverted change in whitespace
tkueeu 7924271
removed notebook files which are now unit tests
tkueeu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import { runInterpreter } from '../src/interpreter/runner.js'; | ||
import { expect, test } from 'vitest'; | ||
|
||
test('identity function', async() => { | ||
const input = ` | ||
fun returnSum(a: number, b: number): number { | ||
print "returnSum called"; | ||
return a + b; | ||
} | ||
|
||
fun identity(a: (number, number) => number): (number, number) => number { | ||
print "identity called"; | ||
return a; | ||
} | ||
|
||
print identity(returnSum)(27, 15); // prints "42"; | ||
`; | ||
|
||
const expectedOutput = ` | ||
identity called | ||
returnSum called | ||
42 | ||
`; | ||
|
||
await runInterpreterAndAssertOutput(input, expectedOutput); | ||
}); | ||
|
||
test('pass reference to function and call it', async() => { | ||
const input = ` | ||
fun aFunction(aLambda: (number) => number, aNumber: number): number { | ||
print "aFunction called"; | ||
return aLambda(aNumber); | ||
} | ||
|
||
fun aTimesTwo(a: number): number { | ||
print "aTimeTwo called"; | ||
return a * 2; | ||
} | ||
|
||
var result = aFunction(aTimesTwo, 9); | ||
print result; | ||
`; | ||
|
||
const expectedOutput = ` | ||
aFunction called | ||
aTimeTwo called | ||
18 | ||
`; | ||
|
||
await runInterpreterAndAssertOutput(input, expectedOutput); | ||
}); | ||
|
||
test('Closure 1', async() => { | ||
const input = ` | ||
// So far fails with: No variable 'outside' defined | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe failing tests should be commented out and marked with a TODO or FIXME comment? |
||
|
||
fun returnFunction(): () => void { | ||
var outside = "outside"; | ||
|
||
fun inner(): void { | ||
print outside; | ||
} | ||
|
||
return inner; | ||
} | ||
|
||
var fn = returnFunction(); | ||
fn(); | ||
`; | ||
|
||
const expectedOutput = ` | ||
`; | ||
|
||
await runInterpreterAndAssertOutput(input, expectedOutput); | ||
}); | ||
|
||
test('Closure 2', async() => { | ||
const input = ` | ||
// So far fails with: No variable 'exponent' defined | ||
|
||
fun power(exponent: number): (number) => number { | ||
fun applyPower(base: number): number { | ||
var current = 1; | ||
for (var i = 0; i < exponent; i = i + 1) { | ||
current = current * base; | ||
} | ||
return current; | ||
} | ||
return applyPower; | ||
} | ||
|
||
var cube = power(3); | ||
|
||
print cube(1); | ||
print cube(2); | ||
print cube(3); | ||
print cube(4); | ||
print cube(5); | ||
`; | ||
|
||
const expectedOutput = ` | ||
`; | ||
|
||
await runInterpreterAndAssertOutput(input, expectedOutput); | ||
}); | ||
|
||
|
||
async function runInterpreterAndAssertOutput(input: string, expectedOutput: string) { | ||
// TODO call valication before ?!? | ||
let output = ""; | ||
await runInterpreter(input, { | ||
log: value => { | ||
output = output.concat(`${value}`); | ||
} | ||
}); | ||
expect(output.replace(/\s/g, "")).toBe(expectedOutput.replace(/\s/g, "")); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import type { LoxProgram } from '../src/language-server/generated/ast.js'; | ||
import { createLoxServices } from '../src/language-server/lox-module.js'; | ||
import { EmptyFileSystem } from 'langium'; | ||
import { parseHelper } from 'langium/test'; | ||
import { test } from 'vitest'; | ||
|
||
|
||
test('parse', async() => { | ||
const services = createLoxServices(EmptyFileSystem).Lox; | ||
const parse = parseHelper<LoxProgram>(services); | ||
|
||
const input = ` | ||
fun returnSum(a: number, b: number): number { | ||
return a + b; | ||
} | ||
|
||
// Closures | ||
|
||
fun identity(a: (number, number) => number): (number, number) => number { | ||
return a; | ||
} | ||
|
||
print identity(returnSum)(1, 2); // prints "3"; | ||
` | ||
|
||
const ast = await parse(input); | ||
ast.parseResult; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not yet a complete test ... |
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"compilerOptions": { | ||
"rootDir": "src", | ||
"outDir": "out" | ||
}, | ||
"include": [ | ||
"src/**/*" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"extends": "./tsconfig.src.json", | ||
"compilerOptions": { | ||
"noEmit": true, | ||
"rootDir": "test" | ||
}, | ||
"references": [{ | ||
"path": "./tsconfig.src.json" | ||
}], | ||
"include": [ | ||
"test/**/*", | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { defineConfig } from 'vitest/config' | ||
|
||
export default defineConfig({ | ||
test: { | ||
coverage: { | ||
provider: 'v8', | ||
reporter: ['text', 'html'], | ||
include: ['src'], | ||
exclude: ['**/generated'], | ||
}, | ||
deps: { | ||
interopDefault: true | ||
}, | ||
include: ['test/*.test.ts'] | ||
} | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was necessary to import less from vscode, otherwise the full initialization breaks unit test