This commit is contained in:
Corey Johnson 2025-10-06 16:37:07 -07:00
parent e0fafc0088
commit a28bdf74c9
13 changed files with 242 additions and 223 deletions

View File

@ -16,7 +16,7 @@ Shrimp is a shell-like scripting language that combines the simplicity of comman
## Current Status & Goals
### Today's Implementation Goals
1. **Interpreter Setup** - Rename evaluator to interpreter for clarity
1. **Interpreter Setup** - Renamed evaluator to interpreter for clarity
2. **Command Execution** - Support calling external commands and built-in functions
3. **Variable Assignment** - Implement assignment with validation using Lezer context tracking

View File

@ -1,7 +1,7 @@
#! /usr/bin/env bun
import { parser } from '../parser/shrimp.js'
import { evaluate } from '../evaluator/evaluator.js'
import { evaluate } from '../interpreter/evaluator.js'
const log = (...args: any[]) => console.log(...args)
log.error = (...args: any[]) => console.error(...args)

View File

@ -1,5 +1,5 @@
import { outputSignal } from '#editor/editor'
import { evaluate } from '#evaluator/evaluator'
import { evaluate } from '#interpreter/evaluator'
import { parser } from '#parser/shrimp'
import { errorMessage, log } from '#utils/utils'
import { keymap } from '@codemirror/view'

View File

@ -1,6 +1,6 @@
import { Tree, type SyntaxNode } from '@lezer/common'
import * as terms from '../parser/shrimp.terms.ts'
import { RuntimeError } from '#evaluator/runtimeError.ts'
import { RuntimeError } from '#interpreter/runtimeError.ts'
import { assert } from 'console'
import { assertNever } from '#utils/utils.tsx'
import { matchingCommands, type CommandShape } from '#editor/commands.ts'

View File

@ -1,7 +1,13 @@
@external propSource highlighting from "./highlight.js"
@top Program { (Expression newline)* }
@skip { space }
@top Program { (line newline)* }
@tokens {
@precedence { Number "-" }
NamedArgPrefix { $[a-z]+ "=" }
Number { "-"? $[0-9]+ ('.' $[0-9]+)? }
Boolean { "true" | "false" }
String { '\'' !["]* '\'' }
@ -9,6 +15,11 @@
space { " " }
leftParen { "(" }
rightParen { ")" }
":"
"fn"
"do"
"end"
"="
"+"[@name=operator]
"-"[@name=operator]
"*"[@name=operator]
@ -16,20 +27,28 @@
}
@external tokens tokenizer from "./tokenizers" { Identifier, Word }
@precedence {
multiplicative @left,
additive @left
call
}
Expression {
line {
FunctionCall |
FunctionCallOrIdentifier |
FunctionDef |
Assignment |
expressionWithoutIdentifier
}
expression {
expressionWithoutIdentifier | Identifier
}
expressionWithoutIdentifier {
BinOp |
ParenExpr |
Word |
String |
Number |
Boolean
valueWithoutIdentifier
}
@ -38,11 +57,11 @@ FunctionCallOrIdentifier {
}
FunctionCall {
Identifier (~ambig space arg)+
Identifier arg+
}
arg {
PositionalArg | NamedArg | IncompleteNamedArg
PositionalArg | NamedArg
}
PositionalArg {
@ -50,29 +69,36 @@ PositionalArg {
}
NamedArg {
Identifier "=" value
NamedArgPrefix value
}
IncompleteNamedArg {
Identifier "="
FunctionDef {
"fn" Params ":" "do" expression "end"
}
Params {
Identifier*
}
Assignment {
Identifier "=" line
}
BinOp {
operand ~ambig space !multiplicative "*" space operand |
operand ~ambig space !multiplicative "/" space operand |
operand ~ambig space !additive "+" space operand |
operand ~ambig space !additive "-" space operand
expression !multiplicative "*" expression |
expression !multiplicative "/" expression |
expression !additive "+" expression |
expression !additive "-" expression
}
operand {
value | BinOp
}
ParenExpr {
leftParen Expression rightParen
leftParen (expressionWithoutIdentifier | FunctionCall | FunctionCallOrIdentifier) rightParen
}
value {
ParenExpr | Identifier | Word | String | Number | Boolean
valueWithoutIdentifier | Identifier
}
valueWithoutIdentifier {
ParenExpr | Word | String | Number | Boolean
}

View File

@ -3,14 +3,16 @@ export const
Identifier = 1,
Word = 2,
Program = 3,
Expression = 4,
FunctionCall = 5,
PositionalArg = 6,
ParenExpr = 7,
String = 8,
Number = 9,
Boolean = 10,
NamedArg = 11,
IncompleteNamedArg = 12,
FunctionCallOrIdentifier = 13,
BinOp = 14
FunctionCall = 4,
PositionalArg = 5,
ParenExpr = 6,
BinOp = 7,
FunctionCallOrIdentifier = 12,
String = 13,
Number = 14,
Boolean = 15,
NamedArg = 16,
NamedArgPrefix = 17,
FunctionDef = 18,
Params = 20,
Assignment = 24

View File

@ -21,7 +21,6 @@ describe('calling functions', () => {
test('call with no args', () => {
expect('tail').toMatchTree(`
Expression
FunctionCallOrIdentifier
Identifier tail
`)
@ -29,7 +28,6 @@ describe('calling functions', () => {
test('call with arg', () => {
expect('tail path').toMatchTree(`
Expression
FunctionCall
Identifier tail
PositionalArg
@ -39,20 +37,18 @@ describe('calling functions', () => {
test('call with arg and named arg', () => {
expect('tail path lines=30').toMatchTree(`
Expression
FunctionCall
Identifier tail
PositionalArg
Identifier path
NamedArg
Identifier lines
NamedArgPrefix lines=
Number 30
`)
})
test('command with arg that is also a command', () => {
expect('tail tail').toMatchTree(`
Expression
FunctionCall
Identifier tail
PositionalArg
@ -60,7 +56,6 @@ describe('calling functions', () => {
`)
expect('tai').toMatchTree(`
Expression
FunctionCallOrIdentifier
Identifier tai
`)
@ -74,27 +69,18 @@ describe('calling functions', () => {
test('Incomplete namedArg', () => {
expect('tail lines=').toMatchTree(`
Expression
FunctionCall
Identifier tail
IncompleteNamedArg
Identifier lines
`)
NamedArg
NamedArgPrefix lines=
`)
})
})
describe('Identifier', () => {
test('fails on underscores and capital letters', () => {
expect('myVar').toFailParse()
expect('underscore_var').toFailParse()
expect('_leadingUnderscore').toFailParse()
expect('trailingUnderscore_').toFailParse()
expect('mixed-123_var').toFailParse()
})
test('parses identifiers with emojis and dashes', () => {
expect('moo-😊-34').toMatchTree(`
Expression
FunctionCallOrIdentifier
Identifier moo-😊-34`)
})
@ -103,9 +89,7 @@ describe('Identifier', () => {
describe('Parentheses', () => {
test('parses expressions with parentheses correctly', () => {
expect('(2 + 3)').toMatchTree(`
Expression
ParenExpr
Expression
BinOp
Number 2
operator +
@ -114,23 +98,45 @@ describe('Parentheses', () => {
test('allows parens in function calls', () => {
expect('echo (3 + 3)').toMatchTree(`
Expression
FunctionCall
Identifier echo
PositionalArg
ParenExpr
Expression
BinOp
Number 3
operator +
Number 3`)
})
test('nested parentheses', () => {
expect('(2 + (1 * 4))').toMatchTree(`
ParenExpr
BinOp
Number 2
operator +
ParenExpr
BinOp
Number 1
operator *
Number 4`)
})
test('Function in parentheses', () => {
expect('4 + (echo 3)').toMatchTree(`
BinOp
Number 4
operator +
ParenExpr
FunctionCall
Identifier echo
PositionalArg
Number 3`)
})
})
describe('BinOp', () => {
test('addition tests', () => {
expect('2 + 3').toMatchTree(`
Expression
BinOp
Number 2
operator +
@ -140,7 +146,6 @@ describe('BinOp', () => {
test('subtraction tests', () => {
expect('5 - 2').toMatchTree(`
Expression
BinOp
Number 5
operator -
@ -150,7 +155,6 @@ describe('BinOp', () => {
test('multiplication tests', () => {
expect('4 * 3').toMatchTree(`
Expression
BinOp
Number 4
operator *
@ -160,7 +164,6 @@ describe('BinOp', () => {
test('division tests', () => {
expect('8 / 2').toMatchTree(`
Expression
BinOp
Number 8
operator /
@ -170,7 +173,6 @@ describe('BinOp', () => {
test('mixed operations with precedence', () => {
expect('2 + 3 * 4 - 5 / 1').toMatchTree(`
Expression
BinOp
BinOp
Number 2
@ -188,86 +190,78 @@ describe('BinOp', () => {
})
})
// describe('Fn', () => {
// test('parses function with single parameter', () => {
// expect('fn x: x + 1').toMatchTree(`
// Function
// keyword fn
// Params
// Identifier x
// colon :
// BinOp
// Identifier x
// operator +
// Number 1`)
// })
describe('Fn', () => {
test('parses function no parameters', () => {
expect('fn: do 1 end').toMatchTree(`
FunctionDef
fn fn
Params
: :
do do
Number 1
end end`)
})
// test('parses function with multiple parameters', () => {
// expect('fn x y: x * y').toMatchTree(`
// Function
// keyword fn
// Params
// Identifier x
// Identifier y
// colon :
// BinOp
// Identifier x
// operator *
// Identifier y`)
// })
test('parses function with single parameter', () => {
expect('fn x: do x + 1 end').toMatchTree(`
FunctionDef
fn fn
Params
Identifier x
: :
do do
BinOp
Identifier x
operator +
Number 1
end end`)
})
// test('parses nested functions', () => {
// expect('fn x: fn y: x + y').toMatchTree(`
// Function
// keyword fn
// Params
// Identifier x
// colon :
// Function
// keyword fn
// Params
// Identifier y
// colon :
// BinOp
// Identifier x
// operator +
// Identifier y`)
// })
// })
test('parses function with multiple parameters', () => {
expect('fn x y: do x * y end').toMatchTree(`
FunctionDef
fn fn
Params
Identifier x
Identifier y
: :
do do
BinOp
Identifier x
operator *
Identifier y
end end`)
})
})
// describe('Identifier', () => {
// test('parses hyphenated identifiers correctly', () => {
// expect('my-var').toMatchTree(`Identifier my-var`)
// expect('double--trouble').toMatchTree(`Identifier double--trouble`)
// })
// })
describe('Assignment', () => {
test('parses assignment with addition', () => {
expect('x = 5 + 3').toMatchTree(`
Assignment
Identifier x
= =
BinOp
Number 5
operator +
Number 3`)
})
// describe('Assignment', () => {
// test('parses assignment with addition', () => {
// expect('x = 5 + 3').toMatchTree(`
// Assignment
// Identifier x
// operator =
// BinOp
// Number 5
// operator +
// Number 3`)
// })
// test('parses assignment with functions', () => {
// expect('add = fn a b: a + b').toMatchTree(`
// Assignment
// Identifier add
// operator =
// Function
// keyword fn
// Params
// Identifier a
// Identifier b
// colon :
// BinOp
// Identifier a
// operator +
// Identifier b`)
// })
// })
test('parses assignment with functions', () => {
expect('add = fn a b: do a + b end').toMatchTree(`
Assignment
Identifier add
= =
FunctionDef
fn fn
Params
Identifier a
Identifier b
: :
do do
BinOp
Identifier a
operator +
Identifier b
end end`)
})
})

View File

@ -4,16 +4,16 @@ import {tokenizer} from "./tokenizers"
import {highlighting} from "./highlight.js"
export const parser = LRParser.deserialize({
version: 14,
states: "$nQQOTOOOQOTO'#CcOfOPO'#CtOqOPO'#CtOOOO'#Cx'#CxO!POPO'#CxO![OPOOOOOO'#C`'#C`O!aOPO'#CoQQOTOOO!fOPO,58}O!|OTO'#CpO#TOPO,58{O#`OQO,59UOOOS,59Z,59ZOOOS-E6m-E6mOOOO1G.i1G.iOOOO'#Ct'#CtO#nOPO'#CtOOOO'#Cb'#CbOOOO'#Cs'#CsOOOO,59[,59[OOOO-E6n-E6nO#|OPO1G.pO$ROTO,59SO$cOTO7+$[OOOO1G.m1G.mOOOO<<Gv<<Gv",
stateData: "$j~OPROQQOWQOXQOYQOiPO~OfhXmSXjSX~OfZOfhXm]Xj]X~OflXmSXjSX~Of]O~Om^O~Oj`O~OQaOWaOXaOYaOiPO~OPbO~P!kOfZOmTajTa~O_gO`gOagObgO~OkhOfhXmhXjhX~OfiO~OPaOf[am[aj[a~P!kOPaO~P!kO",
goto: "#UmPPPPnuz}PPPzzu!XPPPP!a!gPP!m!pPPP!|SWOXRYPVVOPXRdZUQOPXVaZhiQSiVTOPXQXOR_XQ[RRf[ReZWSOPXiQcZRjhUUOPXRki",
nodeNames: "⚠ Identifier Word Program Expression FunctionCall PositionalArg ParenExpr String Number Boolean NamedArg IncompleteNamedArg FunctionCallOrIdentifier BinOp operator operator operator operator",
maxTerm: 29,
states: "&YQVQTOOOnQPO'#DSO!tQUO'#DSO#OQPOOOOQO'#DR'#DRO#oQTO'#CbOOQS'#DP'#DPO#vQTO'#CnOOQO'#C|'#C|O$OQPO'#CvQVQTOOOOQS'#DO'#DOOOQS'#Ca'#CaO$TQTO'#ClOOQS'#C}'#C}OOQS'#Cw'#CwO$[QUO,58zOVQTO,59`O$lQTO,58}O$lQTO,58}O$sQPO,58|O%UQUO'#DSO%]QPO,58|OOQS'#Cx'#CxO%bQTO'#CpO%jQPO,59YOOQS,59b,59bOOQS-E6t-E6tOOQS,59W,59WOOQS-E6u-E6uOOQO1G.z1G.zOOQO'#DS'#DSOOQO1G.i1G.iO%oQPO1G.iOOQS1G.h1G.hOOQS-E6v-E6vO&WQPO1G.tO$lQTO7+$`O&]QPO<<GzOOQOAN=fAN=f",
stateData: "&q~OoOS~OPQOQUO]UO^UO_UOcVOtTO~OWvXXvXYvXZvXxpX~OPZOQUO]UO^UO_UOa]OtTOWvXXvXYvXZvX~OiaOx[X~P!POWbOXbOYcOZcO~OQUO]UO^UO_UOtTO~OPeO~P#^OPgOedP~OxjO~OPZO~P#^OPZOa]OxSawSa~P#^OPoO~P#^OwrOWvXXvXYvXZvX~Ow[X~P!POwrO~OPgOedX~OetO~OWbOXbOYViZVixViwVigVi~OfuO~OWbOXbOYcOZcOgwO~O^Z~",
goto: "$kwPPPPx!Q!V!dPPPPxPPP!QP!mP!rPPP!mP!u!{#SPPP#Y#a#f#nP#}$[UWOYaRfTV^Q`egUOQTY]`abceu_SOTYabcuVWOYaRiVQYORkYS`QeRm`QhVRshSXOYRnaV_Q`eU[Q`eRl]^SOTYabcuXZQ]`eUPOYaQdTVobcuWROTYaQpbQqcRvu",
nodeNames: "⚠ Identifier Word Program FunctionCall PositionalArg ParenExpr BinOp operator operator operator operator FunctionCallOrIdentifier String Number Boolean NamedArg NamedArgPrefix FunctionDef fn Params : do end Assignment =",
maxTerm: 40,
propSources: [highlighting],
skippedNodes: [0],
repeatNodeCount: 2,
tokenData: "%i~R^YZ}pq!Swx!Xxy#]yz#bz{#g{|#l}!O#q!P!Q$d!Q![#y!_!`$i#Y#Z$n#h#i%]~~}~!SOm~~!XOf~~![UOr!Xsw!Xwx!nx;'S!X;'S;=`#V<%lO!X~!sUW~Or!Xsw!Xwx!nx;'S!X;'S;=`#V<%lO!X~#YP;=`<%l!X~#bOi~~#gOj~~#lO_~~#qOa~R#vPbQ!Q![#yP$OQXP!O!P$U!Q![#yP$XP!Q![$[P$aPXP!Q![$[~$iO`~~$nOk~~$qP#T#U$t~$wP#`#a$z~$}P#g#h%Q~%TP#X#Y%W~%]OY~~%`P#f#g%c~%fP#i#j%Q",
repeatNodeCount: 3,
tokenData: ")c~RdYZ!apq!fwx!kxy#oyz#tz{#y{|$O}!O$T!P!Q$v!Q![$]![!]${!_!`%Q#T#W%V#W#X%e#X#Y&P#Y#Z&z#Z#h%V#h#i(s#i#o%V~~!a~!fOx~~!kOo~~!nUOr!ksw!kwx#Qx;'S!k;'S;=`#i<%lO!k~#VU]~Or!ksw!kwx#Qx;'S!k;'S;=`#i<%lO!k~#lP;=`<%l!k~#tOt~~#yOw~~$OOW~~$TOY~~$YPZ~!Q![$]~$bQ^~!O!P$h!Q![$]~$kP!Q![$n~$sP^~!Q![$n~${OX~~%QOe~~%VOi~Q%YQ!_!`%`#T#o%VQ%eOaQR%hS!_!`%`#T#c%V#c#d%t#d#o%VR%yQfP!_!`%`#T#o%VR&SS!_!`%`#T#b%V#b#c&`#c#o%VR&cS!_!`%`#T#W%V#W#X&o#X#o%VR&tQgP!_!`%`#T#o%V~&}T!_!`%`#T#U'^#U#b%V#b#c(h#c#o%V~'aS!_!`%`#T#`%V#`#a'm#a#o%V~'pS!_!`%`#T#g%V#g#h'|#h#o%V~(PS!_!`%`#T#X%V#X#Y(]#Y#o%V~(bQ_~!_!`%`#T#o%VR(mQcP!_!`%`#T#o%V~(vS!_!`%`#T#f%V#f#g)S#g#o%V~)VS!_!`%`#T#i%V#i#j'|#j#o%V",
tokenizers: [0, 1, tokenizer],
topRules: {"Program":[0,3]},
tokenPrec: 0
tokenPrec: 260
})

View File

@ -1,5 +1,5 @@
import { ExternalTokenizer, InputStream, Stack } from '@lezer/lr'
import { Identifier, Word, NamedArg } from './shrimp.terms'
import { Identifier, Word } from './shrimp.terms'
export const tokenizer = new ExternalTokenizer((input: InputStream, stack: Stack) => {
let ch = getFullCodePoint(input, 0)
@ -7,18 +7,15 @@ export const tokenizer = new ExternalTokenizer((input: InputStream, stack: Stack
let pos = getCharSize(ch)
let isValidIdentifier = isLowercaseLetter(ch) || isEmoji(ch)
const canBeWord = stack.canShift(Word)
while (true) {
ch = getFullCodePoint(input, pos)
if (isWhitespace(ch) || ch === -1) break
// Only stop at = if we could parse a NamedArg here
if (ch === 61 /* = */ && isValidIdentifier) {
break // Stop, let grammar handle identifier = value
}
// Track identifier validity
if (!isLowercaseLetter(ch) && !isDigit(ch) && ch !== 45 && !isEmoji(ch)) {
if (!canBeWord) break
isValidIdentifier = false
}

View File

@ -3,7 +3,7 @@ import { Tree, TreeCursor } from '@lezer/common'
import { parser } from '#parser/shrimp'
import { $ } from 'bun'
import { assert } from '#utils/utils'
import { evaluate } from '#evaluator/evaluator'
import { evaluate } from '#interpreter/evaluator'
const regenerateParser = async () => {
let generate = true