-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[VOI-98] Simplify the mechanisms behind cloning and parents (#44)
* WIP * Add classes and move stuff around * WIP * Apply to block * Update snapshots * WIP * Cleanup * Better map cloning? * Remove attributes for a sec * Overoptimize * Add attributes * Cleanup cloning logic * Cleanup maybe * Simplify child list * Simplify list * Cleanup * Cleanup * Use attributes
- Loading branch information
Showing
31 changed files
with
483 additions
and
289 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
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,68 +1,97 @@ | ||
import { idIs, isOp } from "../grammar.js"; | ||
import { Expr, List } from "../../syntax-objects/index.js"; | ||
import { Expr, List, ListValue } from "../../syntax-objects/index.js"; | ||
|
||
export const functionalNotation = (list: List): List => { | ||
const array = list.toArray(); | ||
let isTuple = false; | ||
|
||
const result = list.mapFilter((expr, index, array) => { | ||
if (expr.isList()) return functionalNotation(expr); | ||
if (expr.isWhitespace()) return expr; | ||
const { result } = array.reduce( | ||
(acc, expr, index) => { | ||
if (acc.skip > 0) { | ||
acc.skip--; | ||
return acc; | ||
} | ||
|
||
const nextExpr = array[index + 1]; | ||
if (nextExpr && nextExpr.isList() && !(isOp(expr) || idIs(expr, ","))) { | ||
return processFnCall(expr, nextExpr, array, index); | ||
} | ||
if (expr.isList()) { | ||
acc.result.push(functionalNotation(expr)); | ||
return acc; | ||
} | ||
|
||
if (list.mayBeTuple && idIs(expr, ",")) { | ||
isTuple = true; | ||
} | ||
if (expr.isWhitespace()) { | ||
acc.result.push(expr); | ||
return acc; | ||
} | ||
|
||
return expr; | ||
}); | ||
const nextExpr = array[index + 1]; | ||
|
||
if (isTuple) { | ||
result.insert("tuple"); | ||
result.insert(","); | ||
} | ||
if (nextExpr && nextExpr.isList() && !(isOp(expr) || idIs(expr, ","))) { | ||
return handleNextExpression(acc, expr, nextExpr, array, index); | ||
} | ||
|
||
return result; | ||
if (list.getAttribute("tuple?") && idIs(expr, ",")) { | ||
isTuple = true; | ||
} | ||
|
||
acc.result.push(expr); | ||
return acc; | ||
}, | ||
{ result: [], skip: 0 } as Accumulator | ||
); | ||
|
||
return finalizeResult(result, isTuple); | ||
}; | ||
|
||
const processFnCall = ( | ||
type Accumulator = { result: ListValue[]; skip: number }; | ||
|
||
const handleNextExpression = ( | ||
acc: Accumulator, | ||
expr: Expr, | ||
nextExpr: List, | ||
nextExpr: Expr, | ||
array: Expr[], | ||
index: number | ||
): List => { | ||
if (nextExpr.calls("generics")) { | ||
return processGenerics(expr, array, index); | ||
) => { | ||
if ((nextExpr as List).calls("generics")) { | ||
const generics = nextExpr as List; | ||
const nextNextExpr = array[index + 2]; | ||
if (nextNextExpr && nextNextExpr.isList()) { | ||
acc.result.push(processGenerics(expr, generics, nextNextExpr as List)); | ||
acc.skip = 2; // Skip next two expressions | ||
} else { | ||
acc.result.push(processGenerics(expr, generics)); | ||
acc.skip = 1; // Skip next expression | ||
} | ||
} else { | ||
acc.result.push(processParamList(expr, nextExpr as List)); | ||
acc.skip = 1; // Skip next expression | ||
} | ||
|
||
return processParamList(expr, array, index); | ||
return acc; | ||
}; | ||
|
||
const processGenerics = (expr: Expr, array: Expr[], index: number): List => { | ||
const generics = array.splice(index + 1, 1)[0] as List; | ||
generics.mayBeTuple = false; | ||
const finalizeResult = (result: ListValue[], isTuple: boolean): List => { | ||
if (isTuple) { | ||
result.unshift(","); | ||
result.unshift("tuple"); | ||
} | ||
return new List(result); | ||
}; | ||
|
||
const list = array[index + 1]?.isList() | ||
? (array.splice(index + 1, 1)[0] as List) | ||
: new List({}); | ||
const processGenerics = (expr: Expr, generics: List, params?: List): List => { | ||
generics.setAttribute("tuple?", false); | ||
|
||
list.insert(","); | ||
const list = params || new List([]); | ||
list.insert(expr); | ||
list.mayBeTuple = false; | ||
list.insert(",", 1); | ||
list.setAttribute("tuple?", false); | ||
const functional = functionalNotation(list); | ||
|
||
functional.insert(functionalNotation(generics), 2); | ||
functional.insert(",", 3); | ||
return functional; | ||
}; | ||
|
||
const processParamList = (expr: Expr, array: Expr[], index: number): List => { | ||
const list = array.splice(index + 1, 1)[0] as List; | ||
list.insert(expr); | ||
list.insert(",", 1); | ||
list.mayBeTuple = false; | ||
return functionalNotation(list); | ||
const processParamList = (expr: Expr, params: List): List => { | ||
params.insert(expr); | ||
params.insert(",", 1); | ||
params.setAttribute("tuple?", false); | ||
return functionalNotation(params); | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Syntax Objects | ||
|
||
Syntax objects are data structures that represent concepts within the | ||
language. Such as functions, function calls, variables etc. | ||
|
||
# Guidelines | ||
|
||
- Each Syntax Object should be part of the `Expr` union | ||
- Syntax objects must track their parent-child relationship. A child typically | ||
belongs to a parent when it was directly defined with the parent. I.E. | ||
parameters of functions, expressions / variables of block. These parent | ||
relationships are stored via the parent pointer and must stay up to date | ||
during clones. Use `ChildList` and `ChildMap` data types to help keep | ||
make this easier. | ||
- Resolved values should not be considered a child of the expression they | ||
were resolved from. The type expression (`typeExpr`) of a parameter is | ||
a child of the parameter, but the type it resolves to should never | ||
accidentally be marked as a child of the parameter. Nor should it be | ||
included in the clone (instead, they type resolution can be run again) |
Oops, something went wrong.