From 8172f0218e17e18a80535223c6b97beda195f6ee Mon Sep 17 00:00:00 2001
From: Jeremy Walker There is no information available for this line. There is no information available for this line. There is no information available for this line. There is no information available for this line. Show us your code in Discord and we'll improve this! There is no information available for this line. Show us your code in Discord and we'll improve this! This looped through This looped through This iteration set This iteration set ${
+ this.callee.name.lexeme
+ }${argsDescription}
(which returned ${quoteLiteral(
+ result.value
+ )}
)`
+ }
}
export class ArrayExpression extends Expression {
diff --git a/app/javascript/interpreter/frames.ts b/app/javascript/interpreter/frames.ts
index 7f4ed4143c..afff4f24ef 100644
--- a/app/javascript/interpreter/frames.ts
+++ b/app/javascript/interpreter/frames.ts
@@ -5,10 +5,12 @@ export type FrameExecutionStatus = 'SUCCESS' | 'ERROR'
import type {
EvaluationResult,
EvaluationResultChangeVariableStatement,
+ EvaluationResultIfStatement,
} from './evaluation-result'
import type { ExternalFunction } from './executor'
import {
BinaryExpression,
+ CallExpression,
Expression,
GroupingExpression,
LiteralExpression,
@@ -49,33 +51,41 @@ export function describeFrame(
frame: Frame,
externalFunctions: ExternalFunction[]
): string {
- // These need to come from the exercise.
- const functionDescriptions: Record${frame.result.iterable.name}
array. Each time this line of code is run, it selects the next item from the array and assigns to the ${frame.result.elementName}
variable.${result.iterable.name}
array. Each time this line of code is run, it selects the next item from the array and assigns to the ${frame.result.elementName}
variable.${frame.result.elementName}
to:
`
+ if (result.value) {
+ output += `${JSON.stringify(
- frame.result.value,
- null,
- 2
- )}
${result.elementName}
to:
`
}
return output
}
-function describeExpression(expression: Expression) {
+function describeExpression(expression: Expression, result?: EvaluationResult) {
if (expression instanceof VariableExpression) {
return expression.description()
}
if (expression instanceof LiteralExpression) {
return expression.description()
}
+ if (expression instanceof CallExpression) {
+ return expression.description(result)
+ }
if (expression instanceof GroupingExpression) {
- return describeGroupingExpression(expression)
+ return describeGroupingExpression(expression, result)
}
if (expression instanceof BinaryExpression) {
- return describeBinaryExpression(expression)
+ return describeBinaryExpression(expression, result)
}
if (expression instanceof LogicalExpression) {
- return describeLogicalExpression(expression)
+ return describeLogicalExpression(expression, result)
}
return ''
}
@@ -164,23 +173,26 @@ function describeOperator(operator: string): string {
return ''
}
-function describeBinaryExpression(expression: BinaryExpression): string {
- if (expression instanceof BinaryExpression) {
- const left = describeExpression(expression.left)
- const right = describeExpression(expression.right)
- const operator = describeOperator(expression.operator.type)
- if (isEqualityOperator(expression.operator.type)) {
- return `${left} was ${operator} ${right}`
- } else {
- return `${left} ${operator} ${right}`
- }
+function describeBinaryExpression(
+ expression: BinaryExpression,
+ result?: EvaluationResult
+): string {
+ const left = describeExpression(expression.left, result?.left)
+ const right = describeExpression(expression.right, result?.right)
+ const operator = describeOperator(expression.operator.type)
+ if (isEqualityOperator(expression.operator.type)) {
+ return `${left} was ${operator} ${right}`
+ } else {
+ return `${left} ${operator} ${right}`
}
- return ''
}
-function describeLogicalExpression(expression: LogicalExpression): string {
- const left = describeExpression(expression.left)
- const right = describeExpression(expression.right)
+function describeLogicalExpression(
+ expression: LogicalExpression,
+ result?: EvaluationResult
+): string {
+ const left = describeExpression(expression.left, result?.left)
+ const right = describeExpression(expression.right, result?.right)
if (expression.operator.type == 'AND') {
return `both of these were true:${JSON.stringify(result.value, null, 2)}
` @@ -189,21 +201,30 @@ function describeLogicalExpression(expression: LogicalExpression): string { } } -function describeGroupingExpression(expression: GroupingExpression): string { - return `${describeExpression(expression.inner)}` +function describeGroupingExpression( + expression: GroupingExpression, + result: EvaluationResult +): string { + return `${describeExpression(expression.inner, result)}` } -function describeCondition(expression: Expression): string { - return describeExpression(expression) +function describeCondition( + expression: Expression, + result: EvaluationResultIfStatement +): string { + return describeExpression(expression, result.condition) } function describeIfStatement(frame: FrameWithResult) { const ifStatement = frame.context as IfStatement - const conditionDescription = describeCondition(ifStatement.condition) + const conditionDescription = describeCondition( + ifStatement.condition, + frame.result + ) let output = ` -
This checked whether ${conditionDescription}
-The result was ${frame.result.value}
.
This checked whether ${conditionDescription}
+The result was ${frame.result.value}
.
getName()
(which returned "Jeremy"
)'
+ )
+ })
+ })
+})
diff --git a/test/javascript/interpreter/statement_descriptions.test.ts b/test/javascript/interpreter/statement_descriptions.test.ts
index 2220913069..4366e11df4 100644
--- a/test/javascript/interpreter/statement_descriptions.test.ts
+++ b/test/javascript/interpreter/statement_descriptions.test.ts
@@ -8,6 +8,27 @@ import { SetVariableStatement } from '@/interpreter/statement'
import { describeFrame } from '@/interpreter/frames'
const location = new Location(0, new Span(0, 0), new Span(0, 0))
+const getNameFunction = {
+ name: 'get_name',
+ func: (_interpreter: any) => {
+ return 'Jeremy'
+ },
+ description: '',
+}
+const getTrueFunction = {
+ name: 'get_true',
+ func: (_interpreter: any) => {
+ return true
+ },
+ description: '',
+}
+const getFalseFunction = {
+ name: 'get_false',
+ func: (_interpreter: any) => {
+ return false
+ },
+ description: '',
+}
describe('SetVariableStatement', () => {
describe('description', () => {
@@ -18,6 +39,15 @@ describe('SetVariableStatement', () => {
'This created a new variable called my_name
and sets its value to "Jeremy"
.
This created a new variable called my_name
and sets its value to "Jeremy"
.
This checked whether true
was equal to true
The result was true
.
This checked whether both of these were true:
true
was equal to true
true
was equal to true
The result was true
.
This checked whether get_true()
(which returned true
) was equal to true
The result was true
.
This checked whether true
was equal to get_true()
(which returned true
)
The result was true
.
This checked whether get_true()
(which returned true
) was equal to get_false()
(which returned false
)
The result was false
.
This checked whether both of these were true:
get_true()
(which returned true
) was equal to get_true()
(which returned true
)get_false()
(which returned false
) was equal to get_false()
(which returned false
)The result was true
.