Compare commits
10 Commits
0eca3685f5
...
3c06cac36c
| Author | SHA1 | Date | |
|---|---|---|---|
| 3c06cac36c | |||
| 51b64da106 | |||
| 0dbba4d847 | |||
| 34305b473e | |||
| fd197a2dfc | |||
| ced190488a | |||
| d843071bee | |||
| 40a648cd19 | |||
| 07ffc7df97 | |||
| 3496b29072 |
|
|
@ -1,6 +1,7 @@
|
|||
import { CompilerError } from '#compiler/compilerError.ts'
|
||||
import { parser } from '#parser/shrimp.ts'
|
||||
import * as terms from '#parser/shrimp.terms'
|
||||
import { setGlobals } from '#parser/tokenizer'
|
||||
import type { SyntaxNode, Tree } from '@lezer/common'
|
||||
import { assert, errorMessage } from '#utils/utils'
|
||||
import { toBytecode, type Bytecode, type ProgramItem, bytecodeToString } from 'reefvm'
|
||||
|
|
@ -53,8 +54,9 @@ export class Compiler {
|
|||
bytecode: Bytecode
|
||||
pipeCounter = 0
|
||||
|
||||
constructor(public input: string) {
|
||||
constructor(public input: string, globals?: string[]) {
|
||||
try {
|
||||
if (globals) setGlobals(globals)
|
||||
const cst = parser.parse(input)
|
||||
const errors = checkTreeForErrors(cst)
|
||||
|
||||
|
|
@ -218,6 +220,9 @@ export class Compiler {
|
|||
case '/':
|
||||
instructions.push(['DIV'])
|
||||
break
|
||||
case '%':
|
||||
instructions.push(['MOD'])
|
||||
break
|
||||
default:
|
||||
throw new CompilerError(`Unsupported binary operator: ${opValue}`, op.from, op.to)
|
||||
}
|
||||
|
|
@ -361,7 +366,7 @@ export class Compiler {
|
|||
|
||||
const opValue = input.slice(op.from, op.to)
|
||||
switch (opValue) {
|
||||
case '=':
|
||||
case '==':
|
||||
instructions.push(...leftInstructions, ...rightInstructions, ['EQ'])
|
||||
break
|
||||
|
||||
|
|
|
|||
|
|
@ -38,6 +38,12 @@ describe('compiler', () => {
|
|||
expect('15 / 3').toEvaluateTo(5)
|
||||
})
|
||||
|
||||
test('modulo', () => {
|
||||
expect('44 % 2').toEvaluateTo(0)
|
||||
expect('44 % 3').toEvaluateTo(2)
|
||||
expect('3 % 4').toEvaluateTo(3)
|
||||
})
|
||||
|
||||
test('assign number', () => {
|
||||
expect('x = 5').toEvaluateTo(5)
|
||||
})
|
||||
|
|
@ -105,7 +111,7 @@ describe('compiler', () => {
|
|||
expect(`(10 > 20)`).toEvaluateTo(false)
|
||||
expect(`(4 <= 9)`).toEvaluateTo(true)
|
||||
expect(`(15 >= 20)`).toEvaluateTo(false)
|
||||
expect(`(7 = 7)`).toEvaluateTo(true)
|
||||
expect(`(7 == 7)`).toEvaluateTo(true)
|
||||
expect(`(5 != 5)`).toEvaluateTo(false)
|
||||
expect(`('shave' and 'haircut')`).toEvaluateTo('haircut')
|
||||
expect(`(false and witness)`).toEvaluateTo(false)
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ const operators: Array<Operator> = [
|
|||
{ str: '>=', tokenName: 'Gte' },
|
||||
{ str: '<=', tokenName: 'Lte' },
|
||||
{ str: '!=', tokenName: 'Neq' },
|
||||
{ str: '==', tokenName: 'EqEq' },
|
||||
|
||||
// // Single-char operators
|
||||
{ str: '*', tokenName: 'Star' },
|
||||
|
|
@ -17,6 +18,7 @@ const operators: Array<Operator> = [
|
|||
{ str: '-', tokenName: 'Minus' },
|
||||
{ str: '>', tokenName: 'Gt' },
|
||||
{ str: '<', tokenName: 'Lt' },
|
||||
{ str: '%', tokenName: 'Modulo' },
|
||||
]
|
||||
|
||||
export const operatorTokenizer = new ExternalTokenizer((input: InputStream) => {
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
|
||||
@top Program { item* }
|
||||
|
||||
@external tokens operatorTokenizer from "./operatorTokenizer" { Star, Slash, Plus, Minus, And, Or, Eq, Neq, Lt, Lte, Gt, Gte }
|
||||
@external tokens operatorTokenizer from "./operatorTokenizer" { Star, Slash, Plus, Minus, And, Or, Eq, EqEq, Neq, Lt, Lte, Gt, Gte, Modulo }
|
||||
|
||||
@tokens {
|
||||
@precedence { Number Regex }
|
||||
|
|
@ -33,6 +33,9 @@
|
|||
|
||||
@precedence {
|
||||
pipe @left,
|
||||
or @left,
|
||||
and @left,
|
||||
comparison @left,
|
||||
multiplicative @left,
|
||||
additive @left,
|
||||
call
|
||||
|
|
@ -52,6 +55,7 @@ consumeToTerminator {
|
|||
FunctionDef |
|
||||
Assign |
|
||||
BinOp |
|
||||
ConditionalOp |
|
||||
expressionWithoutIdentifier
|
||||
}
|
||||
|
||||
|
|
@ -129,14 +133,14 @@ SingleLineThenBlock {
|
|||
}
|
||||
|
||||
ConditionalOp {
|
||||
expression Eq expression |
|
||||
expression Neq expression |
|
||||
expression Lt expression |
|
||||
expression Lte expression |
|
||||
expression Gt expression |
|
||||
expression Gte expression |
|
||||
expression And (expression | ConditionalOp) |
|
||||
expression Or (expression | ConditionalOp)
|
||||
expression !comparison EqEq expression |
|
||||
expression !comparison Neq expression |
|
||||
expression !comparison Lt expression |
|
||||
expression !comparison Lte expression |
|
||||
expression !comparison Gt expression |
|
||||
expression !comparison Gte expression |
|
||||
(expression | ConditionalOp) !and And (expression | ConditionalOp) |
|
||||
(expression | ConditionalOp) !or Or (expression | ConditionalOp)
|
||||
}
|
||||
|
||||
Params {
|
||||
|
|
@ -148,6 +152,7 @@ Assign {
|
|||
}
|
||||
|
||||
BinOp {
|
||||
expression !multiplicative Modulo expression |
|
||||
(expression | BinOp) !multiplicative Star (expression | BinOp) |
|
||||
(expression | BinOp) !multiplicative Slash (expression | BinOp) |
|
||||
(expression | BinOp) !additive Plus (expression | BinOp) |
|
||||
|
|
|
|||
|
|
@ -7,45 +7,47 @@ export const
|
|||
And = 5,
|
||||
Or = 6,
|
||||
Eq = 7,
|
||||
Neq = 8,
|
||||
Lt = 9,
|
||||
Lte = 10,
|
||||
Gt = 11,
|
||||
Gte = 12,
|
||||
Identifier = 13,
|
||||
AssignableIdentifier = 14,
|
||||
Word = 15,
|
||||
IdentifierBeforeDot = 16,
|
||||
Do = 17,
|
||||
Program = 18,
|
||||
PipeExpr = 19,
|
||||
FunctionCall = 20,
|
||||
DotGet = 21,
|
||||
Number = 22,
|
||||
ParenExpr = 23,
|
||||
FunctionCallOrIdentifier = 24,
|
||||
BinOp = 25,
|
||||
String = 26,
|
||||
StringFragment = 27,
|
||||
Interpolation = 28,
|
||||
EscapeSeq = 29,
|
||||
Boolean = 30,
|
||||
Regex = 31,
|
||||
Dict = 32,
|
||||
NamedArg = 33,
|
||||
NamedArgPrefix = 34,
|
||||
FunctionDef = 35,
|
||||
Params = 36,
|
||||
colon = 37,
|
||||
keyword = 52,
|
||||
Underscore = 39,
|
||||
Array = 40,
|
||||
Null = 41,
|
||||
ConditionalOp = 42,
|
||||
PositionalArg = 43,
|
||||
IfExpr = 45,
|
||||
SingleLineThenBlock = 47,
|
||||
ThenBlock = 48,
|
||||
ElseIfExpr = 49,
|
||||
ElseExpr = 51,
|
||||
Assign = 53
|
||||
EqEq = 8,
|
||||
Neq = 9,
|
||||
Lt = 10,
|
||||
Lte = 11,
|
||||
Gt = 12,
|
||||
Gte = 13,
|
||||
Modulo = 14,
|
||||
Identifier = 15,
|
||||
AssignableIdentifier = 16,
|
||||
Word = 17,
|
||||
IdentifierBeforeDot = 18,
|
||||
Do = 19,
|
||||
Program = 20,
|
||||
PipeExpr = 21,
|
||||
FunctionCall = 22,
|
||||
DotGet = 23,
|
||||
Number = 24,
|
||||
ParenExpr = 25,
|
||||
FunctionCallOrIdentifier = 26,
|
||||
BinOp = 27,
|
||||
String = 28,
|
||||
StringFragment = 29,
|
||||
Interpolation = 30,
|
||||
EscapeSeq = 31,
|
||||
Boolean = 32,
|
||||
Regex = 33,
|
||||
Dict = 34,
|
||||
NamedArg = 35,
|
||||
NamedArgPrefix = 36,
|
||||
FunctionDef = 37,
|
||||
Params = 38,
|
||||
colon = 39,
|
||||
keyword = 54,
|
||||
Underscore = 41,
|
||||
Array = 42,
|
||||
Null = 43,
|
||||
ConditionalOp = 44,
|
||||
PositionalArg = 45,
|
||||
IfExpr = 47,
|
||||
SingleLineThenBlock = 49,
|
||||
ThenBlock = 50,
|
||||
ElseIfExpr = 51,
|
||||
ElseExpr = 53,
|
||||
Assign = 55
|
||||
|
|
|
|||
|
|
@ -4,24 +4,24 @@ import {operatorTokenizer} from "./operatorTokenizer"
|
|||
import {tokenizer, specializeKeyword} from "./tokenizer"
|
||||
import {trackScope} from "./scopeTracker"
|
||||
import {highlighting} from "./highlight"
|
||||
const spec_Identifier = {__proto__:null,end:76, null:82, if:92, elseif:100, else:104}
|
||||
const spec_Identifier = {__proto__:null,end:80, null:86, if:96, elseif:104, else:108}
|
||||
export const parser = LRParser.deserialize({
|
||||
version: 14,
|
||||
states: "2YQYQbOOO!ZOpO'#CqO#mQcO'#CtO$gOSO'#CvO$uQbO'#ETOOQ`'#DP'#DPOOQa'#C|'#C|O%xQbO'#DUO&}QcO'#DxOOQa'#Dx'#DxO(TQcO'#DwO(lQRO'#CuO(zQcO'#DsO)cQbO'#CsOOQ`'#Dt'#DtO*ZQbO'#DsO*iQbO'#EZOOQ`'#DZ'#DZO+^QRO'#DcOOQ`'#Ds'#DsO+cQQO'#DrOOQ`'#Dr'#DrOOQ`'#Dd'#DdQYQbOOO+kObO,59]O+sQbO'#C}OOQa'#Dw'#DwOOQ`'#DX'#DXOOQ`'#EY'#EYOOQ`'#Dk'#DkO+}QbO,59[O,bQbO'#CxO,jQWO'#CyOOOO'#Dz'#DzOOOO'#De'#DeO-OOSO,59bOOQa,59b,59bOOQ`'#Dg'#DgO-^QbO'#DQO-fQQO,5:oOOQ`'#Df'#DfO-kQbO,59pO-rQQO,59hOOQa,59p,59pO-}QbO,59pO*iQbO,59aO*iQbO,59aO.XQRO,59_O/nQRO'#CuO0OQRO,59_O0[QQO,59_O0aQQO,59_O0iQbO'#DlO0tQbO,59ZO1VQRO,5:uO1^QQO,5:uO1cQbO,59}OOQ`,5:^,5:^OOQ`-E7b-E7bOOQa1G.w1G.wOOQ`,59i,59iOOQ`-E7i-E7iOOOO,59d,59dOOOO,59e,59eOOOO-E7c-E7cOO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Identifier Word IdentifierBeforeDot Do Program PipeExpr FunctionCall DotGet Number ParenExpr FunctionCallOrIdentifier BinOp String StringFragment Interpolation EscapeSeq Boolean Regex Dict NamedArg NamedArgPrefix FunctionDef Params colon keyword Underscore Array Null ConditionalOp PositionalArg operator IfExpr keyword SingleLineThenBlock ThenBlock ElseIfExpr keyword ElseExpr keyword Assign",
|
||||
maxTerm: 93,
|
||||
states: "3UQYQbOOO!ZOpO'#CsO#mQcO'#CvO$jOSO'#CxO$xQbO'#EVOOQ`'#DR'#DROOQa'#DO'#DOO%{QbO'#DWO'QQcO'#DzOOQa'#Dz'#DzO)UQcO'#DyO)}QRO'#CwO*bQcO'#DuO*yQcO'#DuO+[QbO'#CuOOQ`'#Dv'#DvO,SQbO'#DuO,bQbO'#E]OOQ`'#D]'#D]O-VQRO'#DeOOQ`'#Du'#DuO-[QQO'#DtOOQ`'#Dt'#DtOOQ`'#Df'#DfQYQbOOO-dObO,59_O-lQbO'#DPOOQa'#Dy'#DyOOQ`'#DZ'#DZOOQ`'#E['#E[OOQ`'#Dm'#DmO-vQbO,59^O.ZQbO'#CzO.cQWO'#C{OOOO'#D|'#D|OOOO'#Dg'#DgO.wOSO,59dOOQa,59d,59dOOQ`'#Di'#DiO/VQbO'#DSO/_QQO,5:qOOQ`'#Dh'#DhO/dQbO,59rO/kQQO,59jOOQa,59r,59rO/vQbO,59rO,bQbO,59cO,bQbO,59cO,bQbO,59cO,bQbO,59tO,bQbO,59tO,bQbO,59tO0QQRO,59aO0XQRO,59aO0jQRO,59aO0eQQO,59aO0uQQO,59aO0}QbO'#DnO1YQbO,59]O1kQRO,5:wO1rQRO,5:wO1}QbO,5:POOQ`,5:`,5:`OOQ`-E7d-E7dOOQa1G.y1G.yOOQ`,59k,59kOOQ`-E7k-E7kOOOO,59f,59fOOOO,59g,59gOOOO-E7e-E7eOOQa1G/O1G/OOOQ`-E7g-E7gO2XQbO1G0]OOQ`-E7f-E7fO2fQQO1G/UOOQa1G/^1G/^O2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#U#_#c#{#|xkQVWajoz}!O!P!Q!R!S!T!n!s!}#O#X#mR!U^TsRtsTOQW^hjo!_!j!}#O#R#U#_#c#{#|Q#d#UV#h#_#{#|ZnQWo!}#OebOh!_!j#R#U#_#c#{#|",
|
||||
nodeNames: "⚠ Star Slash Plus Minus And Or Eq EqEq Neq Lt Lte Gt Gte Modulo Identifier AssignableIdentifier Word IdentifierBeforeDot Do Program PipeExpr FunctionCall DotGet Number ParenExpr FunctionCallOrIdentifier BinOp String StringFragment Interpolation EscapeSeq Boolean Regex Dict NamedArg NamedArgPrefix FunctionDef Params colon keyword Underscore Array Null ConditionalOp PositionalArg operator IfExpr keyword SingleLineThenBlock ThenBlock ElseIfExpr keyword ElseExpr keyword Assign",
|
||||
maxTerm: 95,
|
||||
context: trackScope,
|
||||
nodeProps: [
|
||||
["closedBy", 37,"end"]
|
||||
["closedBy", 39,"end"]
|
||||
],
|
||||
propSources: [highlighting],
|
||||
skippedNodes: [0],
|
||||
repeatNodeCount: 10,
|
||||
tokenData: "AO~R|OX#{XY$jYZ%TZp#{pq$jqs#{st%ntu'Vuw#{wx'[xy'ayz'zz{#{{|(e|}#{}!O(e!O!P#{!P!Q+X!Q![)S![!]3t!]!^%T!^!}#{!}#O4_#O#P6T#P#Q6Y#Q#R#{#R#S6s#S#T#{#T#Y7^#Y#Z8l#Z#b7^#b#c<z#c#f7^#f#g=q#g#h7^#h#i>h#i#o7^#o#p#{#p#q@`#q;'S#{;'S;=`$d<%l~#{~O#{~~@yS$QUkSOt#{uw#{x#O#{#P;'S#{;'S;=`$d<%lO#{S$gP;=`<%l#{^$qUkS!dYOt#{uw#{x#O#{#P;'S#{;'S;=`$d<%lO#{U%[UkS!vQOt#{uw#{x#O#{#P;'S#{;'S;=`$d<%lO#{^%uZkS!eYOY%nYZ#{Zt%ntu&huw%nwx&hx#O%n#O#P&h#P;'S%n;'S;=`'P<%lO%nY&mS!eYOY&hZ;'S&h;'S;=`&y<%lO&hY&|P;=`<%l&h^'S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;=`$d<%lO#{U?j[kSOt#{uw#{x!_#{!_!`8R!`#O#{#P#T#{#T#i7^#i#j;Y#j#o7^#o;'S#{;'S;=`$d<%lO#{U@gU|QkSOt#{uw#{x#O#{#P;'S#{;'S;=`$d<%lO#{~AOO#P~",
|
||||
tokenizers: [operatorTokenizer, 1, 2, 3, tokenizer, new LocalTokenGroup("[~RP!O!PU~ZO!i~~", 11)],
|
||||
topRules: {"Program":[0,18]},
|
||||
specialized: [{term: 13, get: (value: any, stack: any) => (specializeKeyword(value, stack) << 1), external: specializeKeyword},{term: 13, get: (value: keyof typeof spec_Identifier) => spec_Identifier[value] || -1}],
|
||||
tokenPrec: 1008
|
||||
tokenData: "AO~R|OX#{XY$jYZ%TZp#{pq$jqs#{st%ntu'Vuw#{wx'[xy'ayz'zz{#{{|(e|}#{}!O(e!O!P#{!P!Q+X!Q![)S![!]3t!]!^%T!^!}#{!}#O4_#O#P6T#P#Q6Y#Q#R#{#R#S6s#S#T#{#T#Y7^#Y#Z8l#Z#b7^#b#c<z#c#f7^#f#g=q#g#h7^#h#i>h#i#o7^#o#p#{#p#q@`#q;'S#{;'S;=`$d<%l~#{~O#{~~@yS$QUmSOt#{uw#{x#O#{#P;'S#{;'S;=`$d<%lO#{S$gP;=`<%l#{^$qUmS!fYOt#{uw#{x#O#{#P;'S#{;'S;=`$d<%lO#{U%[UmS!xQOt#{uw#{x#O#{#P;'S#{;'S;=`$d<%lO#{^%uZmS!gYOY%nYZ#{Zt%ntu&huw%nwx&hx#O%n#O#P&h#P;'S%n;'S;=`'P<%lO%nY&mS!gYOY&hZ;'S&h;'S;=`&y<%lO&hY&|P;=`<%l&h^'SP;=`<%l%n~'[O!q~~'aO!o~U'hUmS!lQOt#{uw#{x#O#{#P;'S#{;'S;=`$d<%lO#{U(RUmS!}QOt#{uw#{x#O#{#P;'S#{;'S;=`$d<%lO#{U(jWmSOt#{uw#{x!Q#{!Q![)S![#O#{#P;'S#{;'S;=`$d<%lO#{U)ZYmShQOt#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er, new LocalTokenGroup("[~RP!O!PU~ZO!k~~", 11)],
|
||||
topRules: {"Program":[0,20]},
|
||||
specialized: [{term: 15, get: (value: any, stack: any) => (specializeKeyword(value, stack) << 1), external: specializeKeyword},{term: 15, get: (value: keyof typeof spec_Identifier) => spec_Identifier[value] || -1}],
|
||||
tokenPrec: 1132
|
||||
})
|
||||
|
|
|
|||
|
|
@ -36,6 +36,19 @@ describe('Identifier', () => {
|
|||
FunctionCallOrIdentifier
|
||||
Identifier 𝜋`)
|
||||
})
|
||||
|
||||
test('parses identifiers with queries', () => {
|
||||
expect('even? 20').toMatchTree(`
|
||||
FunctionCall
|
||||
Identifier even?
|
||||
PositionalArg
|
||||
Number 20`)
|
||||
|
||||
expect('even?').toMatchTree(`
|
||||
FunctionCallOrIdentifier
|
||||
Identifier even?`)
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
describe('Unicode Symbol Support', () => {
|
||||
|
|
@ -395,6 +408,15 @@ describe('BinOp', () => {
|
|||
`)
|
||||
})
|
||||
|
||||
test('modulo tests', () => {
|
||||
expect('4 % 3').toMatchTree(`
|
||||
BinOp
|
||||
Number 4
|
||||
Modulo %
|
||||
Number 3
|
||||
`)
|
||||
})
|
||||
|
||||
test('mixed operations with precedence', () => {
|
||||
expect('2 + 3 * 4 - 5 / 1').toMatchTree(`
|
||||
BinOp
|
||||
|
|
@ -571,3 +593,89 @@ describe('Comments', () => {
|
|||
Identifier prop`)
|
||||
})
|
||||
})
|
||||
|
||||
describe('Conditional ops', () => {
|
||||
test('or can be chained', () => {
|
||||
expect(`
|
||||
is-positive = do x:
|
||||
if x == 3 or x == 4 or x == 5:
|
||||
true
|
||||
end
|
||||
end
|
||||
`).toMatchTree(`
|
||||
Assign
|
||||
AssignableIdentifier is-positive
|
||||
Eq =
|
||||
FunctionDef
|
||||
Do do
|
||||
Params
|
||||
Identifier x
|
||||
colon :
|
||||
IfExpr
|
||||
keyword if
|
||||
ConditionalOp
|
||||
ConditionalOp
|
||||
ConditionalOp
|
||||
Identifier x
|
||||
EqEq ==
|
||||
Number 3
|
||||
Or or
|
||||
ConditionalOp
|
||||
Identifier x
|
||||
EqEq ==
|
||||
Number 4
|
||||
Or or
|
||||
ConditionalOp
|
||||
Identifier x
|
||||
EqEq ==
|
||||
Number 5
|
||||
colon :
|
||||
ThenBlock
|
||||
Boolean true
|
||||
keyword end
|
||||
keyword end
|
||||
`)
|
||||
})
|
||||
|
||||
test('and can be chained', () => {
|
||||
expect(`
|
||||
is-positive = do x:
|
||||
if x == 3 and x == 4 and x == 5:
|
||||
true
|
||||
end
|
||||
end
|
||||
`).toMatchTree(`
|
||||
Assign
|
||||
AssignableIdentifier is-positive
|
||||
Eq =
|
||||
FunctionDef
|
||||
Do do
|
||||
Params
|
||||
Identifier x
|
||||
colon :
|
||||
IfExpr
|
||||
keyword if
|
||||
ConditionalOp
|
||||
ConditionalOp
|
||||
ConditionalOp
|
||||
Identifier x
|
||||
EqEq ==
|
||||
Number 3
|
||||
And and
|
||||
ConditionalOp
|
||||
Identifier x
|
||||
EqEq ==
|
||||
Number 4
|
||||
And and
|
||||
ConditionalOp
|
||||
Identifier x
|
||||
EqEq ==
|
||||
Number 5
|
||||
colon :
|
||||
ThenBlock
|
||||
Boolean true
|
||||
keyword end
|
||||
keyword end
|
||||
`)
|
||||
})
|
||||
})
|
||||
|
|
@ -4,12 +4,12 @@ import '../shrimp.grammar' // Importing this so changes cause it to retest!
|
|||
|
||||
describe('if/elseif/else', () => {
|
||||
test('parses single line if', () => {
|
||||
expect(`if y = 1: 'cool' end`).toMatchTree(`
|
||||
expect(`if y == 1: 'cool' end`).toMatchTree(`
|
||||
IfExpr
|
||||
keyword if
|
||||
ConditionalOp
|
||||
Identifier y
|
||||
Eq =
|
||||
EqEq ==
|
||||
Number 1
|
||||
colon :
|
||||
SingleLineThenBlock
|
||||
|
|
|
|||
|
|
@ -6,6 +6,13 @@ export function specializeKeyword(ident: string) {
|
|||
return ident === 'do' ? Do : -1
|
||||
}
|
||||
|
||||
// tell the dotGet searcher about builtin globals
|
||||
export const globals: string[] = []
|
||||
export const setGlobals = (newGlobals: string[]) => {
|
||||
globals.length = 0
|
||||
globals.push(...newGlobals)
|
||||
}
|
||||
|
||||
// The only chars that can't be words are whitespace, apostrophes, closing parens, and EOF.
|
||||
|
||||
export const tokenizer = new ExternalTokenizer(
|
||||
|
|
@ -112,7 +119,7 @@ const consumeWordToken = (
|
|||
}
|
||||
|
||||
// Track identifier validity: must be lowercase, digit, dash, or emoji/unicode
|
||||
if (!isLowercaseLetter(ch) && !isDigit(ch) && ch !== 45 /* - */ && !isEmojiOrUnicode(ch)) {
|
||||
if (!isLowercaseLetter(ch) && !isDigit(ch) && ch !== 45 /* - */ && ch !== 63 /* ? */ && !isEmojiOrUnicode(ch)) {
|
||||
if (!canBeWord) break
|
||||
isValidIdentifier = false
|
||||
}
|
||||
|
|
@ -152,7 +159,7 @@ const checkForDotGet = (input: InputStream, stack: Stack, pos: number): number |
|
|||
|
||||
// If identifier is in scope, this is property access (e.g., obj.prop)
|
||||
// If not in scope, it should be consumed as a Word (e.g., file.txt)
|
||||
return context?.scope.has(identifierText) ? IdentifierBeforeDot : null
|
||||
return context?.scope.has(identifierText) || globals.includes(identifierText) ? IdentifierBeforeDot : null
|
||||
}
|
||||
|
||||
// Decide between AssignableIdentifier and Identifier using grammar state + peek-ahead
|
||||
|
|
|
|||
|
|
@ -43,18 +43,144 @@ export const globalFunctions = {
|
|||
}
|
||||
},
|
||||
|
||||
// type predicates
|
||||
'string?': (v: any) => toValue(v).type === 'string',
|
||||
'number?': (v: any) => toValue(v).type === 'number',
|
||||
'boolean?': (v: any) => toValue(v).type === 'boolean',
|
||||
'array?': (v: any) => toValue(v).type === 'array',
|
||||
'dict?': (v: any) => toValue(v).type === 'dict',
|
||||
'function?': (v: any) => {
|
||||
const t = toValue(v).type
|
||||
return t === 'function' || t === 'native'
|
||||
},
|
||||
'null?': (v: any) => toValue(v).type === 'null',
|
||||
'some?': (v: any) => toValue(v).type !== 'null',
|
||||
|
||||
// boolean/logic
|
||||
not: (v: any) => !v,
|
||||
|
||||
// utilities
|
||||
inc: (n: number) => n + 1,
|
||||
dec: (n: number) => n - 1,
|
||||
identity: (v: any) => v,
|
||||
|
||||
// strings
|
||||
join: (arr: string[], sep: string = ',') => arr.join(sep),
|
||||
split: (str: string, sep: string = ',') => str.split(sep),
|
||||
'to-upper': (str: string) => str.toUpperCase(),
|
||||
'to-lower': (str: string) => str.toLowerCase(),
|
||||
trim: (str: string) => str.trim(),
|
||||
str: {
|
||||
join: (arr: string[], sep: string = ',') => arr.join(sep),
|
||||
split: (str: string, sep: string = ',') => str.split(sep),
|
||||
'to-upper': (str: string) => str.toUpperCase(),
|
||||
'to-lower': (str: string) => str.toLowerCase(),
|
||||
trim: (str: string) => str.trim(),
|
||||
// predicates
|
||||
'starts-with?': (str: string, prefix: string) => str.startsWith(prefix),
|
||||
'ends-with?': (str: string, suffix: string) => str.endsWith(suffix),
|
||||
'contains?': (str: string, substr: string) => str.includes(substr),
|
||||
'empty?': (str: string) => str.length === 0,
|
||||
// transformations
|
||||
replace: (str: string, search: string, replacement: string) => str.replace(search, replacement),
|
||||
'replace-all': (str: string, search: string, replacement: string) => str.replaceAll(search, replacement),
|
||||
slice: (str: string, start: number, end?: number | null) => str.slice(start, end ?? undefined),
|
||||
substring: (str: string, start: number, end?: number | null) => str.substring(start, end ?? undefined),
|
||||
repeat: (str: string, count: number) => str.repeat(count),
|
||||
'pad-start': (str: string, length: number, pad: string = ' ') => str.padStart(length, pad),
|
||||
'pad-end': (str: string, length: number, pad: string = ' ') => str.padEnd(length, pad),
|
||||
lines: (str: string) => str.split('\n'),
|
||||
chars: (str: string) => str.split(''),
|
||||
'index-of': (str: string, search: string) => str.indexOf(search),
|
||||
'last-index-of': (str: string, search: string) => str.lastIndexOf(search),
|
||||
match: (str: string, regex: RegExp) => str.match(regex),
|
||||
'test?': (str: string, regex: RegExp) => regex.test(str),
|
||||
},
|
||||
|
||||
// list
|
||||
list: {
|
||||
slice: (list: any[], start: number, end?: number) => list.slice(start, end),
|
||||
map: async (list: any[], cb: Function) => {
|
||||
let acc: any[] = []
|
||||
for (const value of list) acc.push(await cb(value))
|
||||
return acc
|
||||
},
|
||||
filter: async (list: any[], cb: Function) => {
|
||||
let acc: any[] = []
|
||||
for (const value of list) {
|
||||
if (await cb(value)) acc.push(value)
|
||||
}
|
||||
return acc
|
||||
},
|
||||
reduce: async (list: any[], cb: Function, initial: any) => {
|
||||
let acc = initial
|
||||
for (const value of list) acc = await cb(acc, value)
|
||||
return acc
|
||||
},
|
||||
find: async (list: any[], cb: Function) => {
|
||||
for (const value of list) {
|
||||
if (await cb(value)) return value
|
||||
}
|
||||
return null
|
||||
},
|
||||
// predicates
|
||||
'empty?': (list: any[]) => list.length === 0,
|
||||
'contains?': (list: any[], item: any) => list.includes(item),
|
||||
'any?': async (list: any[], cb: Function) => {
|
||||
for (const value of list) {
|
||||
if (await cb(value)) return true
|
||||
}
|
||||
return false
|
||||
},
|
||||
'all?': async (list: any[], cb: Function) => {
|
||||
for (const value of list) {
|
||||
if (!await cb(value)) return false
|
||||
}
|
||||
return true
|
||||
},
|
||||
// sequence operations
|
||||
reverse: (list: any[]) => list.slice().reverse(),
|
||||
sort: (list: any[], cb?: (a: any, b: any) => number) => list.slice().sort(cb),
|
||||
concat: (...lists: any[][]) => lists.flat(1),
|
||||
flatten: (list: any[], depth: number = 1) => list.flat(depth),
|
||||
unique: (list: any[]) => Array.from(new Set(list)),
|
||||
zip: (list1: any[], list2: any[]) => list1.map((item, i) => [item, list2[i]]),
|
||||
// access
|
||||
first: (list: any[]) => list[0] ?? null,
|
||||
last: (list: any[]) => list[list.length - 1] ?? null,
|
||||
rest: (list: any[]) => list.slice(1),
|
||||
take: (list: any[], n: number) => list.slice(0, n),
|
||||
drop: (list: any[], n: number) => list.slice(n),
|
||||
append: (list: any[], item: any) => [...list, item],
|
||||
prepend: (list: any[], item: any) => [item, ...list],
|
||||
'index-of': (list: any[], item: any) => list.indexOf(item),
|
||||
// utilities
|
||||
sum: (list: any[]) => list.reduce((acc, x) => acc + x, 0),
|
||||
count: async (list: any[], cb: Function) => {
|
||||
let count = 0
|
||||
for (const value of list) {
|
||||
if (await cb(value)) count++
|
||||
}
|
||||
return count
|
||||
},
|
||||
partition: async (list: any[], cb: Function) => {
|
||||
const truthy: any[] = []
|
||||
const falsy: any[] = []
|
||||
for (const value of list) {
|
||||
if (await cb(value)) truthy.push(value)
|
||||
else falsy.push(value)
|
||||
}
|
||||
return [truthy, falsy]
|
||||
},
|
||||
compact: (list: any[]) => list.filter(x => x != null),
|
||||
'group-by': async (list: any[], cb: Function) => {
|
||||
const groups: Record<string, any[]> = {}
|
||||
for (const value of list) {
|
||||
const key = String(await cb(value))
|
||||
if (!groups[key]) groups[key] = []
|
||||
groups[key].push(value)
|
||||
}
|
||||
return groups
|
||||
},
|
||||
},
|
||||
|
||||
// collections
|
||||
at: (collection: any, index: number | string) => collection[index],
|
||||
list: (...args: any[]) => args,
|
||||
dict: (atNamed = {}) => atNamed,
|
||||
slice: (list: any[], start: number, end?: number) => list.slice(start, end),
|
||||
range: (start: number, end: number | null) => {
|
||||
if (end === null) {
|
||||
end = start
|
||||
|
|
@ -66,13 +192,67 @@ export const globalFunctions = {
|
|||
}
|
||||
return result
|
||||
},
|
||||
'empty?': (v: any) => {
|
||||
const value = toValue(v)
|
||||
switch (value.type) {
|
||||
case 'string': case 'array':
|
||||
return value.value.length === 0
|
||||
case 'dict':
|
||||
return value.value.size === 0
|
||||
default:
|
||||
return false
|
||||
}
|
||||
},
|
||||
|
||||
// dict
|
||||
dict: {
|
||||
keys: (dict: Record<string, any>) => Object.keys(dict),
|
||||
values: (dict: Record<string, any>) => Object.values(dict),
|
||||
entries: (dict: Record<string, any>) => Object.entries(dict).map(([k, v]) => ({ key: k, value: v })),
|
||||
'has?': (dict: Record<string, any>, key: string) => key in dict,
|
||||
get: (dict: Record<string, any>, key: string, defaultValue: any = null) => dict[key] ?? defaultValue,
|
||||
merge: (...dicts: Record<string, any>[]) => Object.assign({}, ...dicts),
|
||||
'empty?': (dict: Record<string, any>) => Object.keys(dict).length === 0,
|
||||
map: async (dict: Record<string, any>, cb: Function) => {
|
||||
const result: Record<string, any> = {}
|
||||
for (const [key, value] of Object.entries(dict)) {
|
||||
result[key] = await cb(value, key)
|
||||
}
|
||||
return result
|
||||
},
|
||||
filter: async (dict: Record<string, any>, cb: Function) => {
|
||||
const result: Record<string, any> = {}
|
||||
for (const [key, value] of Object.entries(dict)) {
|
||||
if (await cb(value, key)) result[key] = value
|
||||
}
|
||||
return result
|
||||
},
|
||||
'from-entries': (entries: [string, any][]) => Object.fromEntries(entries),
|
||||
},
|
||||
|
||||
// math
|
||||
math: {
|
||||
abs: (n: number) => Math.abs(n),
|
||||
floor: (n: number) => Math.floor(n),
|
||||
ceil: (n: number) => Math.ceil(n),
|
||||
round: (n: number) => Math.round(n),
|
||||
min: (...nums: number[]) => Math.min(...nums),
|
||||
max: (...nums: number[]) => Math.max(...nums),
|
||||
pow: (base: number, exp: number) => Math.pow(base, exp),
|
||||
sqrt: (n: number) => Math.sqrt(n),
|
||||
random: () => Math.random(),
|
||||
clamp: (n: number, min: number, max: number) => Math.min(Math.max(n, min), max),
|
||||
sign: (n: number) => Math.sign(n),
|
||||
trunc: (n: number) => Math.trunc(n),
|
||||
// predicates
|
||||
'even?': (n: number) => n % 2 === 0,
|
||||
'odd?': (n: number) => n % 2 !== 0,
|
||||
'positive?': (n: number) => n > 0,
|
||||
'negative?': (n: number) => n < 0,
|
||||
'zero?': (n: number) => n === 0,
|
||||
},
|
||||
|
||||
// enumerables
|
||||
map: async (list: any[], cb: Function) => {
|
||||
let acc: any[] = []
|
||||
for (const value of list) acc.push(await cb(value))
|
||||
return acc
|
||||
},
|
||||
each: async (list: any[], cb: Function) => {
|
||||
for (const value of list) await cb(value)
|
||||
return list
|
||||
|
|
|
|||
|
|
@ -3,36 +3,161 @@ import { globalFunctions } from '#prelude'
|
|||
|
||||
describe('string operations', () => {
|
||||
test('to-upper converts to uppercase', async () => {
|
||||
await expect(`to-upper 'hello'`).toEvaluateTo('HELLO', globalFunctions)
|
||||
await expect(`to-upper 'Hello World!'`).toEvaluateTo('HELLO WORLD!', globalFunctions)
|
||||
await expect(`str.to-upper 'hello'`).toEvaluateTo('HELLO', globalFunctions)
|
||||
await expect(`str.to-upper 'Hello World!'`).toEvaluateTo('HELLO WORLD!', globalFunctions)
|
||||
})
|
||||
|
||||
test('to-lower converts to lowercase', async () => {
|
||||
await expect(`to-lower 'HELLO'`).toEvaluateTo('hello', globalFunctions)
|
||||
await expect(`to-lower 'Hello World!'`).toEvaluateTo('hello world!', globalFunctions)
|
||||
await expect(`str.to-lower 'HELLO'`).toEvaluateTo('hello', globalFunctions)
|
||||
await expect(`str.to-lower 'Hello World!'`).toEvaluateTo('hello world!', globalFunctions)
|
||||
})
|
||||
|
||||
test('trim removes whitespace', async () => {
|
||||
await expect(`trim ' hello '`).toEvaluateTo('hello', globalFunctions)
|
||||
await expect(`trim '\\n\\thello\\t\\n'`).toEvaluateTo('hello', globalFunctions)
|
||||
await expect(`str.trim ' hello '`).toEvaluateTo('hello', globalFunctions)
|
||||
await expect(`str.trim '\\n\\thello\\t\\n'`).toEvaluateTo('hello', globalFunctions)
|
||||
})
|
||||
|
||||
test('split divides string by separator', async () => {
|
||||
await expect(`split 'a,b,c' ','`).toEvaluateTo(['a', 'b', 'c'], globalFunctions)
|
||||
await expect(`split 'hello' ''`).toEvaluateTo(['h', 'e', 'l', 'l', 'o'], globalFunctions)
|
||||
await expect(`str.split 'a,b,c' ','`).toEvaluateTo(['a', 'b', 'c'], globalFunctions)
|
||||
await expect(`str.split 'hello' ''`).toEvaluateTo(['h', 'e', 'l', 'l', 'o'], globalFunctions)
|
||||
})
|
||||
|
||||
test('split with comma separator', async () => {
|
||||
await expect(`split 'a,b,c' ','`).toEvaluateTo(['a', 'b', 'c'], globalFunctions)
|
||||
await expect(`str.split 'a,b,c' ','`).toEvaluateTo(['a', 'b', 'c'], globalFunctions)
|
||||
})
|
||||
|
||||
test('join combines array elements', async () => {
|
||||
await expect(`join ['a' 'b' 'c'] '-'`).toEvaluateTo('a-b-c', globalFunctions)
|
||||
await expect(`join ['hello' 'world'] ' '`).toEvaluateTo('hello world', globalFunctions)
|
||||
await expect(`str.join ['a' 'b' 'c'] '-'`).toEvaluateTo('a-b-c', globalFunctions)
|
||||
await expect(`str.join ['hello' 'world'] ' '`).toEvaluateTo('hello world', globalFunctions)
|
||||
})
|
||||
|
||||
test('join with comma separator', async () => {
|
||||
await expect(`join ['a' 'b' 'c'] ','`).toEvaluateTo('a,b,c', globalFunctions)
|
||||
await expect(`str.join ['a' 'b' 'c'] ','`).toEvaluateTo('a,b,c', globalFunctions)
|
||||
})
|
||||
|
||||
test('starts-with? checks string prefix', async () => {
|
||||
await expect(`str.starts-with? 'hello' 'hel'`).toEvaluateTo(true, globalFunctions)
|
||||
await expect(`str.starts-with? 'hello' 'bye'`).toEvaluateTo(false, globalFunctions)
|
||||
})
|
||||
|
||||
test('ends-with? checks string suffix', async () => {
|
||||
await expect(`str.ends-with? 'hello' 'lo'`).toEvaluateTo(true, globalFunctions)
|
||||
await expect(`str.ends-with? 'hello' 'he'`).toEvaluateTo(false, globalFunctions)
|
||||
})
|
||||
|
||||
test('contains? checks for substring', async () => {
|
||||
await expect(`str.contains? 'hello world' 'o w'`).toEvaluateTo(true, globalFunctions)
|
||||
await expect(`str.contains? 'hello' 'bye'`).toEvaluateTo(false, globalFunctions)
|
||||
})
|
||||
|
||||
test('empty? checks if string is empty', async () => {
|
||||
await expect(`str.empty? ''`).toEvaluateTo(true, globalFunctions)
|
||||
await expect(`str.empty? 'hello'`).toEvaluateTo(false, globalFunctions)
|
||||
})
|
||||
|
||||
test('replace replaces first occurrence', async () => {
|
||||
await expect(`str.replace 'hello hello' 'hello' 'hi'`).toEvaluateTo('hi hello', globalFunctions)
|
||||
})
|
||||
|
||||
test('replace-all replaces all occurrences', async () => {
|
||||
await expect(`str.replace-all 'hello hello' 'hello' 'hi'`).toEvaluateTo('hi hi', globalFunctions)
|
||||
})
|
||||
|
||||
test('slice extracts substring', async () => {
|
||||
await expect(`str.slice 'hello' 1 3`).toEvaluateTo('el', globalFunctions)
|
||||
await expect(`str.slice 'hello' 2 null`).toEvaluateTo('llo', globalFunctions)
|
||||
})
|
||||
|
||||
test('repeat repeats string', async () => {
|
||||
await expect(`str.repeat 'ha' 3`).toEvaluateTo('hahaha', globalFunctions)
|
||||
})
|
||||
|
||||
test('pad-start pads beginning', async () => {
|
||||
await expect(`str.pad-start '5' 3 '0'`).toEvaluateTo('005', globalFunctions)
|
||||
})
|
||||
|
||||
test('pad-end pads end', async () => {
|
||||
await expect(`str.pad-end '5' 3 '0'`).toEvaluateTo('500', globalFunctions)
|
||||
})
|
||||
|
||||
test('lines splits by newlines', async () => {
|
||||
await expect(`str.lines 'a\\nb\\nc'`).toEvaluateTo(['a', 'b', 'c'], globalFunctions)
|
||||
})
|
||||
|
||||
test('chars splits into characters', async () => {
|
||||
await expect(`str.chars 'abc'`).toEvaluateTo(['a', 'b', 'c'], globalFunctions)
|
||||
})
|
||||
|
||||
test('index-of finds substring position', async () => {
|
||||
await expect(`str.index-of 'hello world' 'world'`).toEvaluateTo(6, globalFunctions)
|
||||
await expect(`str.index-of 'hello' 'bye'`).toEvaluateTo(-1, globalFunctions)
|
||||
})
|
||||
|
||||
test('last-index-of finds last occurrence', async () => {
|
||||
await expect(`str.last-index-of 'hello hello' 'hello'`).toEvaluateTo(6, globalFunctions)
|
||||
})
|
||||
})
|
||||
|
||||
describe('type predicates', () => {
|
||||
test('string? checks for string type', async () => {
|
||||
await expect(`string? 'hello'`).toEvaluateTo(true, globalFunctions)
|
||||
await expect(`string? 42`).toEvaluateTo(false, globalFunctions)
|
||||
})
|
||||
|
||||
test('number? checks for number type', async () => {
|
||||
await expect(`number? 42`).toEvaluateTo(true, globalFunctions)
|
||||
await expect(`number? 'hello'`).toEvaluateTo(false, globalFunctions)
|
||||
})
|
||||
|
||||
test('boolean? checks for boolean type', async () => {
|
||||
await expect(`boolean? true`).toEvaluateTo(true, globalFunctions)
|
||||
await expect(`boolean? 42`).toEvaluateTo(false, globalFunctions)
|
||||
})
|
||||
|
||||
test('array? checks for array type', async () => {
|
||||
await expect(`array? [1 2 3]`).toEvaluateTo(true, globalFunctions)
|
||||
await expect(`array? 42`).toEvaluateTo(false, globalFunctions)
|
||||
})
|
||||
|
||||
test('dict? checks for dict type', async () => {
|
||||
await expect(`dict? [a=1]`).toEvaluateTo(true, globalFunctions)
|
||||
await expect(`dict? []`).toEvaluateTo(false, globalFunctions)
|
||||
})
|
||||
|
||||
test('null? checks for null type', async () => {
|
||||
await expect(`null? null`).toEvaluateTo(true, globalFunctions)
|
||||
await expect(`null? 42`).toEvaluateTo(false, globalFunctions)
|
||||
})
|
||||
|
||||
test('some? checks for non-null', async () => {
|
||||
await expect(`some? 42`).toEvaluateTo(true, globalFunctions)
|
||||
await expect(`some? null`).toEvaluateTo(false, globalFunctions)
|
||||
})
|
||||
})
|
||||
|
||||
describe('boolean logic', () => {
|
||||
test('not negates value', async () => {
|
||||
await expect(`not true`).toEvaluateTo(false, globalFunctions)
|
||||
await expect(`not false`).toEvaluateTo(true, globalFunctions)
|
||||
await expect(`not 42`).toEvaluateTo(false, globalFunctions)
|
||||
await expect(`not null`).toEvaluateTo(true, globalFunctions)
|
||||
})
|
||||
})
|
||||
|
||||
describe('utilities', () => {
|
||||
test('inc increments by 1', async () => {
|
||||
await expect(`inc 5`).toEvaluateTo(6, globalFunctions)
|
||||
await expect(`inc -1`).toEvaluateTo(0, globalFunctions)
|
||||
})
|
||||
|
||||
test('dec decrements by 1', async () => {
|
||||
await expect(`dec 5`).toEvaluateTo(4, globalFunctions)
|
||||
await expect(`dec 0`).toEvaluateTo(-1, globalFunctions)
|
||||
})
|
||||
|
||||
test('identity returns value as-is', async () => {
|
||||
await expect(`identity 42`).toEvaluateTo(42, globalFunctions)
|
||||
await expect(`identity 'hello'`).toEvaluateTo('hello', globalFunctions)
|
||||
})
|
||||
})
|
||||
|
||||
|
|
@ -64,15 +189,15 @@ describe('introspection', () => {
|
|||
})
|
||||
|
||||
describe('collections', () => {
|
||||
test('list creates array from arguments', async () => {
|
||||
await expect(`list 1 2 3`).toEvaluateTo([1, 2, 3], globalFunctions)
|
||||
await expect(`list 'a' 'b'`).toEvaluateTo(['a', 'b'], globalFunctions)
|
||||
await expect(`list`).toEvaluateTo([], globalFunctions)
|
||||
test('literal array creates array from arguments', async () => {
|
||||
await expect(`[ 1 2 3 ]`).toEvaluateTo([1, 2, 3], globalFunctions)
|
||||
await expect(`['a' 'b']`).toEvaluateTo(['a', 'b'], globalFunctions)
|
||||
await expect(`[]`).toEvaluateTo([], globalFunctions)
|
||||
})
|
||||
|
||||
test('dict creates object from named arguments', async () => {
|
||||
await expect(`dict a=1 b=2`).toEvaluateTo({ a: 1, b: 2 }, globalFunctions)
|
||||
await expect(`dict`).toEvaluateTo({}, globalFunctions)
|
||||
test('literal dict creates object from named arguments', async () => {
|
||||
await expect(`[ a=1 b=2 ]`).toEvaluateTo({ a: 1, b: 2 }, globalFunctions)
|
||||
await expect(`[=]`).toEvaluateTo({}, globalFunctions)
|
||||
})
|
||||
|
||||
test('at retrieves element at index', async () => {
|
||||
|
|
@ -85,8 +210,8 @@ describe('collections', () => {
|
|||
})
|
||||
|
||||
test('slice extracts array subset', async () => {
|
||||
await expect(`slice [1 2 3 4 5] 1 3`).toEvaluateTo([2, 3], globalFunctions)
|
||||
await expect(`slice [1 2 3 4 5] 2 5`).toEvaluateTo([3, 4, 5], globalFunctions)
|
||||
await expect(`list.slice [1 2 3 4 5] 1 3`).toEvaluateTo([2, 3], globalFunctions)
|
||||
await expect(`list.slice [1 2 3 4 5] 2 5`).toEvaluateTo([3, 4, 5], globalFunctions)
|
||||
})
|
||||
|
||||
test('range creates number sequence', async () => {
|
||||
|
|
@ -98,20 +223,189 @@ describe('collections', () => {
|
|||
await expect(`range 3 null`).toEvaluateTo([0, 1, 2, 3], globalFunctions)
|
||||
await expect(`range 0 null`).toEvaluateTo([0], globalFunctions)
|
||||
})
|
||||
|
||||
test('empty? checks if list, dict, string is empty', async () => {
|
||||
await expect(`empty? []`).toEvaluateTo(true, globalFunctions)
|
||||
await expect(`empty? [1]`).toEvaluateTo(false, globalFunctions)
|
||||
|
||||
await expect(`empty? [=]`).toEvaluateTo(true, globalFunctions)
|
||||
await expect(`empty? [a=true]`).toEvaluateTo(false, globalFunctions)
|
||||
|
||||
await expect(`empty? ''`).toEvaluateTo(true, globalFunctions)
|
||||
await expect(`empty? 'cat'`).toEvaluateTo(false, globalFunctions)
|
||||
await expect(`empty? meow`).toEvaluateTo(false, globalFunctions)
|
||||
})
|
||||
|
||||
test('list.filter keeps matching elements', async () => {
|
||||
await expect(`
|
||||
is-positive = do x:
|
||||
x == 3 or x == 4 or x == 5
|
||||
end
|
||||
list.filter [1 2 3 4 5] is-positive
|
||||
`).toEvaluateTo([3, 4, 5], globalFunctions)
|
||||
})
|
||||
|
||||
test('list.reduce accumulates values', async () => {
|
||||
await expect(`
|
||||
add = do acc x:
|
||||
acc + x
|
||||
end
|
||||
list.reduce [1 2 3 4] add 0
|
||||
`).toEvaluateTo(10, globalFunctions)
|
||||
})
|
||||
|
||||
test('list.find returns first match', async () => {
|
||||
await expect(`
|
||||
is-four = do x:
|
||||
x == 4
|
||||
end
|
||||
list.find [1 2 4 5] is-four
|
||||
`).toEvaluateTo(4, globalFunctions)
|
||||
})
|
||||
|
||||
test('list.find returns null if no match', async () => {
|
||||
await expect(`
|
||||
is-ten = do x: x == 10 end
|
||||
list.find [1 2 3] is-ten
|
||||
`).toEvaluateTo(null, globalFunctions)
|
||||
})
|
||||
|
||||
test('list.empty? checks if list is empty', async () => {
|
||||
await expect(`list.empty? []`).toEvaluateTo(true, globalFunctions)
|
||||
await expect(`list.empty? [1]`).toEvaluateTo(false, globalFunctions)
|
||||
})
|
||||
|
||||
test('list.contains? checks for element', async () => {
|
||||
await expect(`list.contains? [1 2 3] 2`).toEvaluateTo(true, globalFunctions)
|
||||
await expect(`list.contains? [1 2 3] 5`).toEvaluateTo(false, globalFunctions)
|
||||
})
|
||||
|
||||
test('list.reverse reverses array', async () => {
|
||||
await expect(`list.reverse [1 2 3]`).toEvaluateTo([3, 2, 1], globalFunctions)
|
||||
})
|
||||
|
||||
test('list.concat combines arrays', async () => {
|
||||
await expect(`list.concat [1 2] [3 4]`).toEvaluateTo([1, 2, 3, 4], globalFunctions)
|
||||
})
|
||||
|
||||
test('list.flatten flattens nested arrays', async () => {
|
||||
await expect(`list.flatten [[1 2] [3 4]] 1`).toEvaluateTo([1, 2, 3, 4], globalFunctions)
|
||||
})
|
||||
|
||||
test('list.unique removes duplicates', async () => {
|
||||
await expect(`list.unique [1 2 2 3 1]`).toEvaluateTo([1, 2, 3], globalFunctions)
|
||||
})
|
||||
|
||||
test('list.zip combines two arrays', async () => {
|
||||
await expect(`list.zip [1 2] [3 4]`).toEvaluateTo([[1, 3], [2, 4]], globalFunctions)
|
||||
})
|
||||
|
||||
test('list.first returns first element', async () => {
|
||||
await expect(`list.first [1 2 3]`).toEvaluateTo(1, globalFunctions)
|
||||
await expect(`list.first []`).toEvaluateTo(null, globalFunctions)
|
||||
})
|
||||
|
||||
test('list.last returns last element', async () => {
|
||||
await expect(`list.last [1 2 3]`).toEvaluateTo(3, globalFunctions)
|
||||
await expect(`list.last []`).toEvaluateTo(null, globalFunctions)
|
||||
})
|
||||
|
||||
test('list.rest returns all but first', async () => {
|
||||
await expect(`list.rest [1 2 3]`).toEvaluateTo([2, 3], globalFunctions)
|
||||
})
|
||||
|
||||
test('list.take returns first n elements', async () => {
|
||||
await expect(`list.take [1 2 3 4 5] 3`).toEvaluateTo([1, 2, 3], globalFunctions)
|
||||
})
|
||||
|
||||
test('list.drop skips first n elements', async () => {
|
||||
await expect(`list.drop [1 2 3 4 5] 2`).toEvaluateTo([3, 4, 5], globalFunctions)
|
||||
})
|
||||
|
||||
test('list.append adds to end', async () => {
|
||||
await expect(`list.append [1 2] 3`).toEvaluateTo([1, 2, 3], globalFunctions)
|
||||
})
|
||||
|
||||
test('list.prepend adds to start', async () => {
|
||||
await expect(`list.prepend [2 3] 1`).toEvaluateTo([1, 2, 3], globalFunctions)
|
||||
})
|
||||
|
||||
test('list.index-of finds element index', async () => {
|
||||
await expect(`list.index-of [1 2 3] 2`).toEvaluateTo(1, globalFunctions)
|
||||
await expect(`list.index-of [1 2 3] 5`).toEvaluateTo(-1, globalFunctions)
|
||||
})
|
||||
|
||||
test('list.any? checks if any element matches', async () => {
|
||||
await expect(`
|
||||
gt-three = do x: x > 3 end
|
||||
list.any? [1 2 4 5] gt-three
|
||||
`).toEvaluateTo(true, globalFunctions)
|
||||
await expect(`
|
||||
gt-ten = do x: x > 10 end
|
||||
list.any? [1 2 3] gt-ten
|
||||
`).toEvaluateTo(false, globalFunctions)
|
||||
})
|
||||
|
||||
test('list.all? checks if all elements match', async () => {
|
||||
await expect(`
|
||||
positive = do x: x > 0 end
|
||||
list.all? [1 2 3] positive
|
||||
`).toEvaluateTo(true, globalFunctions)
|
||||
await expect(`
|
||||
positive = do x: x > 0 end
|
||||
list.all? [1 -2 3] positive
|
||||
`).toEvaluateTo(false, globalFunctions)
|
||||
})
|
||||
|
||||
test('list.sum adds all numbers', async () => {
|
||||
await expect(`list.sum [1 2 3 4]`).toEvaluateTo(10, globalFunctions)
|
||||
await expect(`list.sum []`).toEvaluateTo(0, globalFunctions)
|
||||
})
|
||||
|
||||
test('list.count counts matching elements', async () => {
|
||||
await expect(`
|
||||
gt-two = do x: x > 2 end
|
||||
list.count [1 2 3 4 5] gt-two
|
||||
`).toEvaluateTo(3, globalFunctions)
|
||||
})
|
||||
|
||||
test('list.partition splits array by predicate', async () => {
|
||||
await expect(`
|
||||
gt-two = do x: x > 2 end
|
||||
list.partition [1 2 3 4 5] gt-two
|
||||
`).toEvaluateTo([[3, 4, 5], [1, 2]], globalFunctions)
|
||||
})
|
||||
|
||||
test('list.compact removes null values', async () => {
|
||||
await expect(`list.compact [1 null 2 null 3]`).toEvaluateTo([1, 2, 3], globalFunctions)
|
||||
})
|
||||
|
||||
test('list.group-by groups by key function', async () => {
|
||||
await expect(`
|
||||
get-type = do x:
|
||||
if (string? x):
|
||||
'str'
|
||||
else:
|
||||
'num'
|
||||
end
|
||||
end
|
||||
list.group-by ['a' 1 'b' 2] get-type
|
||||
`).toEvaluateTo({ str: ['a', 'b'], num: [1, 2] }, globalFunctions)
|
||||
})
|
||||
})
|
||||
|
||||
describe('enumerables', () => {
|
||||
test('map transforms array elements', async () => {
|
||||
await expect(`
|
||||
double = do x: x * 2 end
|
||||
map [1 2 3] double
|
||||
list.map [1 2 3] double
|
||||
`).toEvaluateTo([2, 4, 6], globalFunctions)
|
||||
})
|
||||
|
||||
test('map handles empty array', async () => {
|
||||
await expect(`
|
||||
double = do x: x * 2 end
|
||||
map [] double
|
||||
list.map [] double
|
||||
`).toEvaluateTo([], globalFunctions)
|
||||
})
|
||||
|
||||
|
|
@ -132,6 +426,152 @@ describe('enumerables', () => {
|
|||
})
|
||||
})
|
||||
|
||||
describe('dict operations', () => {
|
||||
test('dict.keys returns all keys', async () => {
|
||||
const result = await (async () => {
|
||||
const { Compiler } = await import('#compiler/compiler')
|
||||
const { run, fromValue } = await import('reefvm')
|
||||
const { setGlobals } = await import('#parser/tokenizer')
|
||||
setGlobals(Object.keys(globalFunctions))
|
||||
const c = new Compiler('dict.keys [a=1 b=2 c=3]')
|
||||
const r = await run(c.bytecode, globalFunctions)
|
||||
return fromValue(r)
|
||||
})()
|
||||
// Check that all expected keys are present (order may vary)
|
||||
expect(result.sort()).toEqual(['a', 'b', 'c'])
|
||||
})
|
||||
|
||||
test('dict.values returns all values', async () => {
|
||||
const result = await (async () => {
|
||||
const { Compiler } = await import('#compiler/compiler')
|
||||
const { run, fromValue } = await import('reefvm')
|
||||
const { setGlobals } = await import('#parser/tokenizer')
|
||||
setGlobals(Object.keys(globalFunctions))
|
||||
const c = new Compiler('dict.values [a=1 b=2]')
|
||||
const r = await run(c.bytecode, globalFunctions)
|
||||
return fromValue(r)
|
||||
})()
|
||||
// Check that all expected values are present (order may vary)
|
||||
expect(result.sort()).toEqual([1, 2])
|
||||
})
|
||||
|
||||
test('dict.has? checks for key', async () => {
|
||||
await expect(`dict.has? [a=1 b=2] 'a'`).toEvaluateTo(true, globalFunctions)
|
||||
await expect(`dict.has? [a=1 b=2] 'c'`).toEvaluateTo(false, globalFunctions)
|
||||
})
|
||||
|
||||
test('dict.get retrieves value with default', async () => {
|
||||
await expect(`dict.get [a=1] 'a' 0`).toEvaluateTo(1, globalFunctions)
|
||||
await expect(`dict.get [a=1] 'b' 99`).toEvaluateTo(99, globalFunctions)
|
||||
})
|
||||
|
||||
test('dict.empty? checks if dict is empty', async () => {
|
||||
await expect(`dict.empty? [=]`).toEvaluateTo(true, globalFunctions)
|
||||
await expect(`dict.empty? [a=1]`).toEvaluateTo(false, globalFunctions)
|
||||
})
|
||||
|
||||
test('dict.merge combines dicts', async () => {
|
||||
await expect(`dict.merge [a=1] [b=2]`).toEvaluateTo({ a: 1, b: 2 }, globalFunctions)
|
||||
})
|
||||
|
||||
test('dict.map transforms values', async () => {
|
||||
await expect(`
|
||||
double = do v k: v * 2 end
|
||||
dict.map [a=1 b=2] double
|
||||
`).toEvaluateTo({ a: 2, b: 4 }, globalFunctions)
|
||||
})
|
||||
|
||||
test('dict.filter keeps matching entries', async () => {
|
||||
await expect(`
|
||||
gt-one = do v k: v > 1 end
|
||||
dict.filter [a=1 b=2 c=3] gt-one
|
||||
`).toEvaluateTo({ b: 2, c: 3 }, globalFunctions)
|
||||
})
|
||||
|
||||
test('dict.from-entries creates dict from array', async () => {
|
||||
await expect(`dict.from-entries [['a' 1] ['b' 2]]`).toEvaluateTo({ a: 1, b: 2 }, globalFunctions)
|
||||
})
|
||||
})
|
||||
|
||||
describe('math operations', () => {
|
||||
test('math.abs returns absolute value', async () => {
|
||||
await expect(`math.abs -5`).toEvaluateTo(5, globalFunctions)
|
||||
await expect(`math.abs 5`).toEvaluateTo(5, globalFunctions)
|
||||
})
|
||||
|
||||
test('math.floor rounds down', async () => {
|
||||
await expect(`math.floor 3.7`).toEvaluateTo(3, globalFunctions)
|
||||
})
|
||||
|
||||
test('math.ceil rounds up', async () => {
|
||||
await expect(`math.ceil 3.2`).toEvaluateTo(4, globalFunctions)
|
||||
})
|
||||
|
||||
test('math.round rounds to nearest', async () => {
|
||||
await expect(`math.round 3.4`).toEvaluateTo(3, globalFunctions)
|
||||
await expect(`math.round 3.6`).toEvaluateTo(4, globalFunctions)
|
||||
})
|
||||
|
||||
test('math.min returns minimum', async () => {
|
||||
await expect(`math.min 5 2 8 1`).toEvaluateTo(1, globalFunctions)
|
||||
})
|
||||
|
||||
test('math.max returns maximum', async () => {
|
||||
await expect(`math.max 5 2 8 1`).toEvaluateTo(8, globalFunctions)
|
||||
})
|
||||
|
||||
test('math.pow computes power', async () => {
|
||||
await expect(`math.pow 2 3`).toEvaluateTo(8, globalFunctions)
|
||||
})
|
||||
|
||||
test('math.sqrt computes square root', async () => {
|
||||
await expect(`math.sqrt 16`).toEvaluateTo(4, globalFunctions)
|
||||
})
|
||||
|
||||
test('math.even? checks if even', async () => {
|
||||
await expect(`math.even? 4`).toEvaluateTo(true, globalFunctions)
|
||||
await expect(`math.even? 5`).toEvaluateTo(false, globalFunctions)
|
||||
})
|
||||
|
||||
test('math.odd? checks if odd', async () => {
|
||||
await expect(`math.odd? 5`).toEvaluateTo(true, globalFunctions)
|
||||
await expect(`math.odd? 4`).toEvaluateTo(false, globalFunctions)
|
||||
})
|
||||
|
||||
test('math.positive? checks if positive', async () => {
|
||||
await expect(`math.positive? 5`).toEvaluateTo(true, globalFunctions)
|
||||
await expect(`math.positive? -5`).toEvaluateTo(false, globalFunctions)
|
||||
await expect(`math.positive? 0`).toEvaluateTo(false, globalFunctions)
|
||||
})
|
||||
|
||||
test('math.negative? checks if negative', async () => {
|
||||
await expect(`math.negative? -5`).toEvaluateTo(true, globalFunctions)
|
||||
await expect(`math.negative? 5`).toEvaluateTo(false, globalFunctions)
|
||||
})
|
||||
|
||||
test('math.zero? checks if zero', async () => {
|
||||
await expect(`math.zero? 0`).toEvaluateTo(true, globalFunctions)
|
||||
await expect(`math.zero? 5`).toEvaluateTo(false, globalFunctions)
|
||||
})
|
||||
|
||||
test('math.clamp restricts value to range', async () => {
|
||||
await expect(`math.clamp 5 0 10`).toEvaluateTo(5, globalFunctions)
|
||||
await expect(`math.clamp -5 0 10`).toEvaluateTo(0, globalFunctions)
|
||||
await expect(`math.clamp 15 0 10`).toEvaluateTo(10, globalFunctions)
|
||||
})
|
||||
|
||||
test('math.sign returns sign of number', async () => {
|
||||
await expect(`math.sign 5`).toEvaluateTo(1, globalFunctions)
|
||||
await expect(`math.sign -5`).toEvaluateTo(-1, globalFunctions)
|
||||
await expect(`math.sign 0`).toEvaluateTo(0, globalFunctions)
|
||||
})
|
||||
|
||||
test('math.trunc truncates decimal', async () => {
|
||||
await expect(`math.trunc 3.7`).toEvaluateTo(3, globalFunctions)
|
||||
await expect(`math.trunc -3.7`).toEvaluateTo(-3, globalFunctions)
|
||||
})
|
||||
})
|
||||
|
||||
// describe('echo', () => {
|
||||
// test('echo returns null value', async () => {
|
||||
// await expect(`echo 'hello' 'world'`).toEvaluateTo(null, globalFunctions)
|
||||
|
|
|
|||
|
|
@ -1,9 +1,10 @@
|
|||
import { expect } from 'bun:test'
|
||||
import { parser } from '#parser/shrimp'
|
||||
import { setGlobals } from '#parser/tokenizer'
|
||||
import { $ } from 'bun'
|
||||
import { assert, errorMessage } from '#utils/utils'
|
||||
import { Compiler } from '#compiler/compiler'
|
||||
import { run, VM, type TypeScriptFunction } from 'reefvm'
|
||||
import { run, VM } from 'reefvm'
|
||||
import { treeToString, VMResultToValue } from '#utils/tree'
|
||||
|
||||
const regenerateParser = async () => {
|
||||
|
|
@ -30,7 +31,7 @@ await regenerateParser()
|
|||
// Type declaration for TypeScript
|
||||
declare module 'bun:test' {
|
||||
interface Matchers<T> {
|
||||
toMatchTree(expected: string): T
|
||||
toMatchTree(expected: string, globals?: Record<string, any>): T
|
||||
toMatchExpression(expected: string): T
|
||||
toFailParse(): T
|
||||
toEvaluateTo(expected: unknown, globals?: Record<string, any>): Promise<T>
|
||||
|
|
@ -39,9 +40,10 @@ declare module 'bun:test' {
|
|||
}
|
||||
|
||||
expect.extend({
|
||||
toMatchTree(received: unknown, expected: string) {
|
||||
toMatchTree(received: unknown, expected: string, globals?: Record<string, any>) {
|
||||
assert(typeof received === 'string', 'toMatchTree can only be used with string values')
|
||||
|
||||
if (globals) setGlobals(Object.keys(globals))
|
||||
const tree = parser.parse(received)
|
||||
const actual = treeToString(tree, received)
|
||||
const normalizedExpected = trimWhitespace(expected)
|
||||
|
|
@ -97,6 +99,7 @@ expect.extend({
|
|||
assert(typeof received === 'string', 'toEvaluateTo can only be used with string values')
|
||||
|
||||
try {
|
||||
if (globals) setGlobals(Object.keys(globals))
|
||||
const compiler = new Compiler(received)
|
||||
const result = await run(compiler.bytecode, globals)
|
||||
let value = VMResultToValue(result)
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user