-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add text language * lint * add hardcode plugin * use compilationOptions func * ensure names are preserved * cleanup * implement no:hardcode test flag
- Loading branch information
1 parent
1bfaed1
commit 01923e4
Showing
17 changed files
with
192 additions
and
12 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { programToSpine } from "../common/Spine"; | ||
import { | ||
type Node, | ||
block, | ||
functionCall, | ||
isBuiltinIdent, | ||
isOfKind, | ||
} from "../IR"; | ||
import { readsFromInput } from "../common/symbols"; | ||
import { PolygolfError } from "../common/errors"; | ||
import { compilationOptions, compileVariant } from "../common/compile"; | ||
import javascriptLanguage from "../languages/javascript"; | ||
import { required } from "../common/Language"; | ||
import { addVarDeclarations } from "../plugins/block"; | ||
|
||
const javascriptForInterpreting = { | ||
...javascriptLanguage, | ||
phases: [ | ||
...javascriptLanguage.phases, | ||
required(addVarDeclarations, { | ||
name: "instrument", | ||
visit(node, spine) { | ||
const parent = spine.parent?.node; | ||
if ( | ||
parent !== undefined && | ||
spine.pathFragment === "body" && | ||
isOfKind("While", "ForEach", "ForEachKey", "ForCLike")(parent) && | ||
(node.kind !== "Block" || | ||
node.children[0].kind !== "FunctionCall" || | ||
!isBuiltinIdent("instrument")(node.children[0].func)) | ||
) { | ||
return block([functionCall("instrument"), node]); | ||
} | ||
}, | ||
}), | ||
], | ||
}; | ||
|
||
const outputCache = new Map<Node, unknown>(); | ||
|
||
export function getOutput(program: Node) { | ||
if (!outputCache.has(program)) { | ||
try { | ||
outputCache.set(program, _getOutput(program)); | ||
} catch (e) { | ||
outputCache.set(program, e); | ||
} | ||
} | ||
const res = outputCache.get(program); | ||
if (typeof res === "string") return res; | ||
throw res; | ||
} | ||
|
||
function _getOutput(program: Node): string { | ||
const spine = programToSpine(program); | ||
if (spine.someNode(readsFromInput)) | ||
throw new PolygolfError("Program reads from input."); | ||
const jsCode = compileVariant( | ||
program, | ||
compilationOptions({ level: "nogolf" }), | ||
javascriptForInterpreting, | ||
); | ||
if (typeof jsCode.result !== "string") { | ||
throw jsCode.result; | ||
} | ||
let output = ""; | ||
function print(x: string) { | ||
output += x + "\n"; | ||
} | ||
function write(x: string) { | ||
output += x; | ||
} | ||
const start = Date.now(); | ||
function instrument() { | ||
if (Date.now() - start > 500) | ||
throw new PolygolfError("Program took too long to interpret."); | ||
} | ||
/* eslint-disable */ | ||
new Function("print", "write", "instrument", jsCode.result)( | ||
print, | ||
write, | ||
instrument, | ||
); | ||
/* eslint-enable */ | ||
return output; | ||
} |
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
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
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 |
---|---|---|
|
@@ -166,7 +166,7 @@ if ($a != 0) { | |
$a <- 1; | ||
``` | ||
|
||
```python | ||
```python no:hardcode | ||
a=1 | ||
if a:print(a) | ||
a=1 | ||
|
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,11 @@ | ||
import { getOutput } from "../../interpreter"; | ||
import type { Language } from "../../common/Language"; | ||
|
||
const textLanguage: Language = { | ||
name: "Text", | ||
extension: "txt", | ||
phases: [], | ||
emitter: getOutput, | ||
}; | ||
|
||
export default textLanguage; |
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,25 @@ | ||
# Text | ||
|
||
```polygolf | ||
% Printing | ||
println "Hello, World!"; | ||
% Looping | ||
for $i 0 10 { | ||
println_int $i; | ||
}; | ||
``` | ||
|
||
```txt | ||
Hello, World! | ||
0 | ||
1 | ||
2 | ||
3 | ||
4 | ||
5 | ||
6 | ||
7 | ||
8 | ||
9 | ||
``` |
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
Oops, something went wrong.