The ThreeAddressCode Interpreter is a tool for running 3AC instructions in the browser. It is a true interpreter for Three Address Code Instructions. Instead of compiling from source language to machine language, it interprets directly in JavaScript.
Copy the ThreeAddressCode.min.js inside your working directory and insert the script tag inside your HTML document.
npm install --save tacinterpreter
The following code segment will produce an alert with result 7.
const ThreeAddressCode = require('tacinterpreter');
let interpreter = new ThreeAddressCode();
interpreter.run("print 3+4", [], function(out) {
alert(out);
});
Takes Config object to configure the intepreter.
Syntax
var interpreter = new ThreeAddressCode([config]);
Method to execute the code segment with the given inputs.
-
Parameters
Code lines separated by the separater defined in the config object
List of inputs referenced in the code segment from p0 to pn.
Pipe stdout each time a print statement is hit, return Print Object.
Handles error handling, return Error Object.
-
Syntax
interpreter.run(code, [inputs, [prints, [error]]]);
-
Example
interpreter.run("print 3+4\nprint 2*3");
Method to execute a single code line with the given inputs.
-
Parameters
Code lines separated by the separater defined in the config object
List of inputs referenced in the code segment from p0 to pn.
Pipe stdout each time a print statement is hit, return Print Object.
Handles error handling, return Error Object.
Clears the heap memory to start from the beginning.
-
Syntax
interpreter.run_line(code, [inputs, [print, [error, [clearHeap]]]);
-
Example
interpreter.run_line("print 3+4");
Method to add a single code line to the list of code lines.
-
Parameters
single code line.
-
Syntax
interpreter.add(code);
-
Example
interpreter.add("print 3+9");
Method to retrieve variables from the heap or to evaluate expression. Returns the value of the varibale stored in heap or the result of an expression.
-
Parameters
single code line or a variable name
-
Syntax
interpreter.get(code);
-
Example
interpreter.get("A");
Method to retrieve results of print statements. Returns list of print objects.
-
Syntax
interpreter.output();
Method to retrieve Heap object from the memory. Debug property must be set in config object
-
Syntax
interpreter.debug();
Shows additional information about errors, warnings, prints and program execution. Default value is false.
Stop execution if error occured in the code. Default value is false.
Used to separate code lines inside code string. Default value is '\n'.
Contains the result of the expression with the print statement.
Contains the line number of the print statement.
Contains the Error message for readibility.
Contains the line number of the instruction that produced the error.
Contains an interger value unique to each error type.
- 100 - Undefined Variable
- 101 - Missing Operand
- 102 - Unknown operator
- 104 - Invalid Pointer Reference
- 105 - Invalid Type Conversion
- 106 - Undefined Label
- 107 - Invalid Method call
- 108 - Array index out of bound
- 120 - Unknown Error Type
Unary and Binary Operations with optional assignment. Variables can be pointers, references and indexed assignments.
[variable = ] expression operator expression
[variable = ] operator expression
array[index] = expression
variable2 = &variable1
*variable2 = expression
[variable3 =] *variable2
// Stores 7 in a
a = 3 + 4
// Stores 21 in b
b = a * 3
// Stores -21 in c
c = -b
// prints 63
9 * 7
// Create an array with only one index '0' with value '10'
arr[0] = 10
// makes 'h' a pointer pointing to the address of 'a'
h = &a
// modifies the value pointed to by 'h'
*h = 25
Identifier that identifies a labeled statement.
label: [statement]
here: a = 6+7
Unconditional jumps to a predefined label.
goto label
here: print 8*7
...
goto here
Determine the flow of control based on condition.
if expression statement
if 5 > 3 print 1
A method can be defined with the keyword BeginFunc and ended with EndFunc.
Create a new method with the name 'methodname' and with number of acceptable parameters 'n'.
Set the boundary for the method code segment. Everything between BeginFunc statement and EndFUnc statement is considered a part of the method body.
Set a parameter to be passed to the method. Must be called before calling the method. Parameters can be accessed inside the method body with variable names from p0 to pn where n is the number of total parameters.
Calls a method with the name 'methodname' and acceptable parameters 'n'. The result returned by the method will be stored in the variable 'out' and will be retained until the next method call. Methods must be declared before calling. If there are more paramertes than required for calling the method, the paramertes added first will be send over to the method and removed.
Halts the method execution and returns the value of the expression (if given).
BeginFunc methodname, n
statements
EndFunc
param p0
param p1
...
param pn
call methodname, n
print out
This code segment defines a method that returns the multiplication of two numbers.
BeginFunc sum, 2
return p0 * p1
EndFunc
param 6
param 7*5
call sum, 2
print out