Skip to content

Commit

Permalink
reduce strictness of scm-slang parser for FULL_SCHEME
Browse files Browse the repository at this point in the history
  • Loading branch information
s-kybound committed Nov 3, 2024
1 parent 1c80862 commit 3cabbb8
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/alt-langs/scheme/scm-slang
55 changes: 43 additions & 12 deletions src/cse-machine/scheme-macros.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,18 +103,33 @@ export function schemeEval(
// find the first matching transformer
for (const transformer of transformers) {
// check if the transformer matches the list
if (match(command, transformer.pattern, transformer.literals)) {
// if it does, apply the transformer
const transformedMacro = macro_transform(command as List, transformer)
control.push(transformedMacro as ControlItem)
return
try {
if (match(command, transformer.pattern, transformer.literals)) {
// if it does, apply the transformer
const transformedMacro = macro_transform(command as List, transformer)
control.push(transformedMacro as ControlItem)
return
}
} catch (e) {
return handleRuntimeError(
context,
new errors.ExceptionError(
new Error(
'Error in macro-expanding ' +
elem.sym +
'! Are the template and pattern well formed?'
)
)
)
}
}

// there is an error if we get to here
return handleRuntimeError(
context,
new errors.ExceptionError(new Error('No matching transformer found for macro'))
new errors.ExceptionError(
new Error('No matching transformer found for macro ' + elem.sym)
)
)
}

Expand Down Expand Up @@ -457,20 +472,36 @@ export function schemeEval(
// find the first matching transformer
for (const transformer of transformers) {
// check if the transformer matches the list
if (match(command, transformer.pattern, transformer.literals)) {
// if it does, apply the transformer
const transformedMacro = macro_transform(command, transformer)
control.push(transformedMacro as ControlItem)
return
try {
if (match(command, transformer.pattern, transformer.literals)) {
// if it does, apply the transformer
const transformedMacro = macro_transform(command, transformer)
control.push(transformedMacro as ControlItem)
return
}
} catch (e) {
return handleRuntimeError(
context,
new errors.ExceptionError(
new Error(
'Error in macro-expanding ' +
command.sym +
'! Are the template and pattern well formed?'
)
)
)
}
}

// there is an error if we get to here
return handleRuntimeError(
context,
new errors.ExceptionError(new Error('No matching transformer found for macro'))
new errors.ExceptionError(
new Error('No matching transformer found for macro ' + command.sym)
)
)
}

// get the value of the symbol from the environment
// associated with this symbol.
const encodedName = encode(command.sym)
Expand Down
26 changes: 26 additions & 0 deletions src/cse-machine/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,24 @@ import Closure from './closure'
import { Continuation, isCallWithCurrentContinuation } from './continuations'
import { isEval } from './scheme-macros'
import { _Symbol } from '../alt-langs/scheme/scm-slang/src/stdlib/base'
import { is_number } from '../alt-langs/scheme/scm-slang/src/stdlib/core-math'

/**
* Typeguard for commands to check if they are scheme values.
*
* @param command A ControlItem
* @returns true if the ControlItem is a scheme value, false otherwise.
*/
export const isSchemeValue = (command: ControlItem): boolean => {
return (
command === null ||
typeof command === 'string' ||
typeof command === 'boolean' ||
isArray(command) ||
command instanceof _Symbol ||
is_number(command)
)
}

/**
* Typeguard for Instr to distinguish between program statements and instructions.
Expand All @@ -31,6 +49,10 @@ import { _Symbol } from '../alt-langs/scheme/scm-slang/src/stdlib/base'
* @returns true if the ControlItem is an instruction and false otherwise.
*/
export const isInstr = (command: ControlItem): command is Instr => {
// this prevents us from reading properties of null
if (isSchemeValue(command)) {
return false
}
return (command as Instr).instrType !== undefined
}

Expand All @@ -41,6 +63,10 @@ export const isInstr = (command: ControlItem): command is Instr => {
* @returns true if the ControlItem is a Node or StatementSequence, false if it is an instruction.
*/
export const isNode = (command: ControlItem): command is Node => {
// this prevents us from reading properties of null
if (isSchemeValue(command)) {
return false
}
return (command as Node).type !== undefined
}

Expand Down

0 comments on commit 3cabbb8

Please sign in to comment.