-
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 coco function call syntaxes (#377)
* add two coco function call syntaxes * add ↦ op alias & add test * only consider |> in coco for chars scoring * add infix calls to coco * add implicit multiplication * remove temp test * reuse py detokenize in coco * fix ambiguous py emit
- Loading branch information
1 parent
9b09c6f
commit f6610f3
Showing
9 changed files
with
166 additions
and
31 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
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 |
---|---|---|
@@ -1,32 +1,40 @@ | ||
import { builtin, listType, op, succ, textType } from "../../IR"; | ||
import { mapOps } from "../../plugins/ops"; | ||
import type { Language } from "../../common/Language"; | ||
import { search, type Language } from "../../common/Language"; | ||
import pythonLanguage from "../python"; | ||
import { CoconutEmitter } from "./emit"; | ||
import { | ||
useImplicitFunctionCalls, | ||
useInfixFunctionCalls, | ||
usePipesWhenChars, | ||
} from "./plugins"; | ||
|
||
const coconutLanguage: Language = { | ||
name: "Coconut", | ||
extension: "coco", | ||
emitter: new CoconutEmitter(), | ||
phases: pythonLanguage.phases.map((phase) => ({ | ||
mode: phase.mode, | ||
plugins: phase.plugins.map((plugin) => | ||
plugin.name === "useSysArgv" | ||
? { | ||
...mapOps({ | ||
argv: () => builtin("os.sys.argv[1:]"), | ||
phases: [ | ||
...pythonLanguage.phases.map((phase) => ({ | ||
mode: phase.mode, | ||
plugins: phase.plugins.map((plugin) => | ||
plugin.name === "useSysArgv" | ||
? { | ||
...mapOps({ | ||
argv: () => builtin("os.sys.argv[1:]"), | ||
|
||
"at[argv]": (a) => | ||
op["at[List]"]( | ||
{ ...builtin("os.sys.argv"), type: listType(textType()) }, | ||
succ(a), | ||
), | ||
}), | ||
name: "useOsSysArgv", | ||
} | ||
: plugin, | ||
), | ||
})), | ||
"at[argv]": (a) => | ||
op["at[List]"]( | ||
{ ...builtin("os.sys.argv"), type: listType(textType()) }, | ||
succ(a), | ||
), | ||
}), | ||
name: "useOsSysArgv", | ||
} | ||
: plugin, | ||
), | ||
})), | ||
search(useImplicitFunctionCalls, usePipesWhenChars, useInfixFunctionCalls), | ||
], | ||
}; | ||
|
||
export default coconutLanguage; |
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,53 @@ | ||
import type { CompilationContext } from "@/common/compile"; | ||
import { type Node, infix, isIdent, isInt } from "../../IR"; | ||
|
||
function isAllowedAsImplicitArg(node: Node): boolean { | ||
return ( | ||
isIdent()(node) || | ||
(node.kind === "PropertyCall" && isIdent()(node.object)) || | ||
isInt()(node) || | ||
(node.kind === "Infix" && | ||
node.name === "**" && | ||
isAllowedAsImplicitArg(node.args[0]) && | ||
isInt()(node.args[1])) | ||
); | ||
} | ||
|
||
export function useImplicitFunctionCalls(node: Node) { | ||
if (node.kind === "FunctionCall") { | ||
if (node.args.length >= 1 && node.args.every(isAllowedAsImplicitArg)) { | ||
return infix("", node.func, ...(node.args as [Node, ...Node[]])); | ||
} | ||
} | ||
if ( | ||
node.kind === "Infix" && | ||
node.name === "*" && | ||
node.args.slice(1).every(isAllowedAsImplicitArg) | ||
) { | ||
return infix("", ...node.args); | ||
} | ||
} | ||
|
||
export function useInfixFunctionCalls(node: Node) { | ||
if ( | ||
node.kind === "FunctionCall" && | ||
isIdent()(node.func) && | ||
node.args.length === 2 | ||
) { | ||
return infix("`", node.args[0], node.func, node.args[1]); | ||
} | ||
} | ||
|
||
export function usePipesWhenChars( | ||
node: Node, | ||
_: unknown, | ||
context: CompilationContext, | ||
) { | ||
if (context.options.objective !== "chars") { | ||
context.skipChildren(); | ||
return; | ||
} | ||
if (node.kind === "FunctionCall" && node.args.length === 1) { | ||
return infix("|>", node.args[0], node.func); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -94,7 +94,7 @@ $b:Int <- 0; | |
|
||
```python | ||
a=b=0 | ||
a!=5 and b==6 | ||
a!=5and b==6 | ||
a=b=0 | ||
``` | ||
|
||
|
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