Start lipsum command

This commit is contained in:
Garrett Mills 2025-11-11 20:50:28 -06:00
parent 144d90e871
commit c437958406
2 changed files with 39 additions and 4 deletions

View File

@ -1,10 +1,15 @@
import {LexInput} from '../lexer.js'
import {ExpectedEndOfInputError, InvalidVariableNameError, UnexpectedEndOfInputError} from "../parse.js";
import {
ExpectedEndOfInputError,
InvalidVariableNameError,
IsNotKeywordError,
UnexpectedEndOfInputError
} from "../parse.js";
export type StrLVal = { term: 'variable', name: string }
export type StrTerm =
{ term: 'string', value: string }
{ term: 'string', value: string, literal?: true }
| StrLVal
export class ParseContext {
@ -25,7 +30,7 @@ export class ParseContext {
popTerm(): StrTerm {
if ( !this.inputs.length ) {
throw new UnexpectedEndOfInputError('Unexpected end of input. Expected term.');
throw new UnexpectedEndOfInputError('Unexpected end of input. Expected term.')
}
const input = this.inputs.shift()!
@ -40,7 +45,19 @@ export class ParseContext {
}
// Otherwise, parse it as a string literal:
return { term: 'string', value: input.value }
return { term: 'string', value: input.value, literal: input.literal }
}
popKeywordInSet<T extends string[]>(options: T) {
if ( !this.inputs.length ) {
throw new UnexpectedEndOfInputError('Unexpected end of input. Expected one of: ' + options.join(', '))
}
const input = this.inputs.shift()!
if ( input.literal || !options.includes(input.value) ) {
throw new IsNotKeywordError('Unexpected term: ' + input.value + ' (expected one of: ' + options.join(', ') + ')')
}
}
popLVal(): StrLVal {

18
src/vm/commands/lipsum.ts Normal file
View File

@ -0,0 +1,18 @@
import {Command, ParseContext, StrTerm} from './command.js'
import {LexInput} from '../lexer.js'
export class Lipsum extends Command<{ length: StrTerm }> {
attemptParse(context: ParseContext): { length: StrTerm } {
return {
length: context.popTerm(),
}
}
getDisplayName(): string {
return 'lipsum'
}
isParseCandidate(token: LexInput): boolean {
return this.isKeyword(token, 'lipsum')
}
}