Skip to content

WIP carnage #7479

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import { evaluateFunction, interpret } from '@/interpreter/interpreter'
import {
evaluateFunction,
evaluateFunctions,
interpret,
Interpreter,
} from '@/interpreter/interpreter'
import { type Project } from '@/components/bootcamp/SolveExercisePage/utils/exerciseMap'
import type { Exercise } from '../../exercises/Exercise'
import { AnimationTimeline } from '../../AnimationTimeline/AnimationTimeline'
import { generateExpects } from './generateExpects'
import { TestRunnerOptions } from '@/components/bootcamp/types/TestRunner'
import { filteredStdLibFunctions } from '@/interpreter/stdlib'
import { fn } from 'jquery'

/**
This is of type TestCallback
Expand Down Expand Up @@ -42,7 +48,16 @@ export function execProjectTest(
})

let evaluated
if (testData.function) {
if (testData.functions) {
const interpreter = new Interpreter(options.studentCode, context)
interpreter.compile()
evaluated = interpreter.execute()

const functions = testData.functions.map((fnData) => {
return { name: fnData[0], args: fnData[1] || [] }
})
evaluated = evaluateFunctions(options.studentCode, context, functions)
} else if (testData.function) {
evaluated = evaluateFunction(
options.studentCode,
context,
Expand All @@ -54,6 +69,7 @@ export function execProjectTest(
}

const { frames } = evaluated
console.log('frames!', frames)

const { animations } = exercise
const animationTimeline = buildAnimationTimeline(exercise, frames, animations)
Expand Down
5 changes: 3 additions & 2 deletions app/javascript/components/bootcamp/types/Tasks.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ declare type TaskTest = {
name: string
slug: string
data: any
function: string
params: string[]
function?: string
functions?: SetupFunction[]
params?: string[]
imageSlug?: string
matcher?: string
expected?: string
Expand Down
73 changes: 55 additions & 18 deletions app/javascript/interpreter/interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ export interface SomethingWithLocation {
location: Location
}

type FunctionCall = {
name: string
args: any[]
}

export type Toggle = 'ON' | 'OFF'

export type LanguageFeatures = {
Expand Down Expand Up @@ -93,6 +98,15 @@ export function evaluateFunction(
interpreter.compile()
return interpreter.evaluateFunction(functionCall, ...args)
}
export function evaluateFunctions(
sourceCode: string,
context: EvaluationContext = {},
functions: FunctionCall[]
): EvaluateFunctionResult {
const interpreter = new Interpreter(sourceCode, context)
interpreter.compile()
return interpreter.evaluateFunctions(functions)
}

export class Interpreter {
private readonly parser: Parser
Expand Down Expand Up @@ -153,30 +167,53 @@ export class Interpreter {
name: string,
...args: any[]
): EvaluateFunctionResult {
const callingCode = `${name}(${args
.map((arg) => JSON.stringify(arg))
.join(', ')})`

// Create a new parser with wrapTopLevelStatements set to false
// and use it to generate the calling statements.
const callingStatements = new Parser(
this.externalFunctions.map((f) => f.name),
this.languageFeatures,
false
).parse(callingCode)

if (callingStatements.length !== 1)
this.error('CouldNotEvaluateFunction', Location.unknown, {
callingStatements,
})
return this.evaluateFunctions([{ name, args }])
}

public evaluateFunctions(functions: FunctionCall[]): EvaluateFunctionResult {
const executor = new Executor(
this.sourceCode,
this.languageFeatures,
this.externalFunctions
)
executor.execute(this.statements)
return executor.evaluateSingleExpression(callingStatements[0])

let result: EvaluateFunctionResult = {
value: undefined,
frames: [],
error: null,
callExpressions: [],
}

functions.forEach((fn) => {
const callingCode = `${fn.name}(${fn.args
.map((arg) => JSON.stringify(arg))
.join(', ')})`

// Create a new parser with wrapTopLevelStatements set to false
// and use it to generate the calling statements.
const callingStatements = new Parser(
this.externalFunctions.map((f) => f.name),
this.languageFeatures,
false
).parse(callingCode)

if (callingStatements.length !== 1)
this.error('CouldNotEvaluateFunction', Location.unknown, {
callingStatements,
})

executor.execute(this.statements)
const evalResult = executor.evaluateSingleExpression(callingStatements[0])
result = {
value: evalResult.value,
frames: result.frames.concat(evalResult.frames),
error: evalResult.error,
}
if (evalResult.error) {
return result
}
})
return result
}

private error(
Expand Down
Loading