Environment

Environment
Instance Members
set(name, value, tracker, location)
get(name, location)
assign(name, value, location)

Environment

Class representing an environment for variable storage and lookup.

new Environment(parent: (Environment | null))
Parameters
parent ((Environment | null) = null) The parent environment.
Instance Members
set(name, value, tracker, location)
get(name, location)
assign(name, value, location)

InterpreterVisitor

InterpreterVisitor class is responsible for interpreting and executing code. It manages the environment, variable tracking, and console output.

new InterpreterVisitor()

Extends BaseVisitor

Instance Members
Environment
Symbols
Console
prevContinue
visitExpression(node)
visitLiteral(node)
visitArithmetic(node)
visitRelational(node)
visitLogical(node)
visitUnary(node)
visitPrint(node)
visitBlock(node)
visitIf(node)
visitCase(node)
visitSwitch(node)
visitTernary(node)
visitWhile(node)
visitFor(node)
visitForEach(node)
visitContinue(node)
visitBreak(node)
visitReturn(node)
visitVarDeclaration(node)
visitVarValue(node)
visitVarAssign(node)
visitGroup(node)
visitExpressionStatement(node)
visitCallee(node)
visitFuncDeclaration(node)
visitStructDeclaration(node)
visitInstance(node)
visitGet(node)
visitSet(node)
visitArrayInstance(node)

forEach

Registers native functions (e.g., parseInt, toString) in the environment.

forEach

Type: void

Location

Location

Type: Object

Properties
start (Object)
end (Object)

Expression

new Expression(options: Object)
Parameters
options (Object)
Instance Members
location
accept(visitor)

constructor

constructor(options: Object)
Parameters
options (Object)
Name Description
options.value any The value of the literal.
options.type string The type of the literal.

value

The value of the literal.

value

Type: any

type

The type of the literal.

type

Type: string

accept

accept(visitor: BaseVisitor)
Parameters
visitor (BaseVisitor)

constructor

constructor(options: Object)
Parameters
options (Object)
Name Description
options.exp Expression The expression inside the group.

exp

The expression inside the group.

exp

Type: Expression

accept

accept(visitor: BaseVisitor)
Parameters
visitor (BaseVisitor)

constructor

constructor(options: Object)
Parameters
options (Object)
Name Description
options.id string The name of the variable.

id

The name of the variable.

id

Type: string

accept

accept(visitor: BaseVisitor)
Parameters
visitor (BaseVisitor)

constructor

constructor(options: Object)
Parameters
options (Object)
Name Description
options.op string The operator of the unary expression.
options.exp Expression The right expression of the unary expression.

op

The operator of the unary expression.

op

Type: string

exp

The right expression of the unary expression.

exp

Type: Expression

accept

accept(visitor: BaseVisitor)
Parameters
visitor (BaseVisitor)

constructor

constructor(options: Object)
Parameters
options (Object)
Name Description
options.op string The operator of the arithmetic expression.
options.left Expression The left expression of the arithmetic expression.
options.right Expression The right expression of the arithmetic expression.

op

The operator of the arithmetic expression.

op

Type: string

left

The left expression of the arithmetic expression.

left

Type: Expression

The right expression of the arithmetic expression.

right

Type: Expression

accept

accept(visitor: BaseVisitor)
Parameters
visitor (BaseVisitor)

constructor

constructor(options: Object)
Parameters
options (Object)
Name Description
options.op string The operator of the relational expression.
options.left Expression The left expression of the relational expression.
options.right Expression The right expression of the relational expression.

op

The operator of the relational expression.

op

Type: string

left

The left expression of the relational expression.

left

Type: Expression

The right expression of the relational expression.

right

Type: Expression

accept

accept(visitor: BaseVisitor)
Parameters
visitor (BaseVisitor)

constructor

constructor(options: Object)
Parameters
options (Object)
Name Description
options.op string The operator of the logical expression.
options.left Expression The left expression of the logical expression.
options.right Expression The right expression of the logical expression.

op

The operator of the logical expression.

op

Type: string

left

The left expression of the logical expression.

left

Type: Expression

The right expression of the logical expression.

right

Type: Expression

accept

accept(visitor: BaseVisitor)
Parameters
visitor (BaseVisitor)

constructor

constructor(options: Object)
Parameters
options (Object)
Name Description
options.cond Expression The condition of the ternary expression.
options.trueExp Expression The true expression of the ternary expression.
options.falseExp Expression The false expression of the ternary expression.

cond

The condition of the ternary expression.

cond

Type: Expression

trueExp

The true expression of the ternary expression.

trueExp

Type: Expression

falseExp

The false expression of the ternary expression.

falseExp

Type: Expression

accept

accept(visitor: BaseVisitor)
Parameters
visitor (BaseVisitor)

constructor

constructor(options: Object)
Parameters
options (Object)
Name Description
options.id string The name of the variable.
options.sig string The assignation of the variable.
options.assign Expression The expression to assign to the variable.

id

The name of the variable.

id

Type: string

sig

The assignation of the variable.

sig

Type: string

assign

The expression to assign to the variable.

assign

Type: Expression

accept

accept(visitor: BaseVisitor)
Parameters
visitor (BaseVisitor)

constructor

constructor(options: Object)
Parameters
options (Object)
Name Description
options.exp (Expression | undefined) The expression to return.

exp

The expression to return.

exp

Type: (Expression | undefined)

accept

accept(visitor: BaseVisitor)
Parameters
visitor (BaseVisitor)

constructor

constructor(options: Object)
Parameters
options (Object)

accept

accept(visitor: BaseVisitor)
Parameters
visitor (BaseVisitor)

constructor

constructor(options: Object)
Parameters
options (Object)

accept

accept(visitor: BaseVisitor)
Parameters
visitor (BaseVisitor)

constructor

constructor(options: Object)
Parameters
options (Object)
Name Description
options.cond Expression The expression of the case.
options.stmt Array<Expression> The instructions of the case.

cond

The expression of the case.

cond

Type: Expression

stmt

The instructions of the case.

stmt

Type: Array<Expression>

accept

accept(visitor: BaseVisitor)
Parameters
visitor (BaseVisitor)

constructor

constructor(options: Object)
Parameters
options (Object)
Name Description
options.cond Expression The expression of the switch.
options.cases Array<Case> The cases of the switch.
options.def Case The default case of the switch.

cond

The expression of the switch.

cond

Type: Expression

cases

The cases of the switch.

cases

Type: Array<Case>

def

The default case of the switch.

def

Type: Case

accept

accept(visitor: BaseVisitor)
Parameters
visitor (BaseVisitor)

constructor

constructor(options: Object)
Parameters
options (Object)
Name Description
options.init Expression The initialization of the for loop.
options.cond Expression The condition of the for loop.
options.update Expression The increment of the for loop.
options.stmt Block The instructions of the for loop.

init

The initialization of the for loop.

init

Type: Expression

cond

The condition of the for loop.

cond

Type: Expression

update

The increment of the for loop.

update

Type: Expression

stmt

The instructions of the for loop.

stmt

Type: Block

accept

accept(visitor: BaseVisitor)
Parameters
visitor (BaseVisitor)

constructor

constructor(options: Object)
Parameters
options (Object)
Name Description
options.vd Expression The name of the variable.
options.array Expression The expression to iterate.
options.stmt Block The instructions of the for each loop.

vd

The name of the variable.

vd

Type: Expression

array

The expression to iterate.

array

Type: Expression

stmt

The instructions of the for each loop.

stmt

Type: Block

accept

accept(visitor: BaseVisitor)
Parameters
visitor (BaseVisitor)

constructor

constructor(options: Object)
Parameters
options (Object)
Name Description
options.cond Expression The condition of the while loop.
options.stmt Block The instructions of the while loop.

cond

The condition of the while loop.

cond

Type: Expression

stmt

The instructions of the while loop.

stmt

Type: Block

accept

accept(visitor: BaseVisitor)
Parameters
visitor (BaseVisitor)

constructor

constructor(options: Object)
Parameters
options (Object)
Name Description
options.cond Expression The condition of the if.
options.stmtThen Expression The instructions of the if.
options.stmtElse Expression The else of the if.

cond

The condition of the if.

cond

Type: Expression

stmtThen

The instructions of the if.

stmtThen

Type: Expression

stmtElse

The else of the if.

stmtElse

Type: Expression

accept

accept(visitor: BaseVisitor)
Parameters
visitor (BaseVisitor)

constructor

constructor(options: Object)
Parameters
options (Object)
Name Description
options.stmt Array<Expression> The instructions of the block.

stmt

The instructions of the block.

stmt

Type: Array<Expression>

accept

accept(visitor: BaseVisitor)
Parameters
visitor (BaseVisitor)

constructor

constructor(options: Object)
Parameters
options (Object)
Name Description
options.exp Array<Expression> The expression to print.

exp

The expression to print.

exp

Type: Array<Expression>

accept

accept(visitor: BaseVisitor)
Parameters
visitor (BaseVisitor)

constructor

constructor(options: Object)
Parameters
options (Object)
Name Description
options.exp Expression The expression of the statement.

exp

The expression of the statement.

exp

Type: Expression

accept

accept(visitor: BaseVisitor)
Parameters
visitor (BaseVisitor)

constructor

constructor(options: Object)
Parameters
options (Object)
Name Description
options.type string The type of the variable.
options.id string The name of the variable.
options.value (Expression | null) The expression of the variable.

type

The type of the variable.

type

Type: string

id

The name of the variable.

id

Type: string

value

The expression of the variable.

value

Type: (Expression | null)

accept

accept(visitor: BaseVisitor)
Parameters
visitor (BaseVisitor)

constructor

constructor(options: Object)
Parameters
options (Object)
Name Description
options.callee Expression The name of the callee.
options.args Array<Expression> The arguments of the callee.

callee

The name of the callee.

callee

Type: Expression

args

The arguments of the callee.

args

Type: Array<Expression>

accept

accept(visitor: BaseVisitor)
Parameters
visitor (BaseVisitor)

constructor

constructor(options: Object)
Parameters
options (Object)
Name Description
options.type string The type of the function.
options.id string The name of the function.
options.params Array<Expression> The parameters of the function.
options.block Block The body of the function.

type

The type of the function.

type

Type: string

id

The name of the function.

id

Type: string

params

The parameters of the function.

params

Type: Array<Expression>

block

The body of the function.

block

Type: Block

accept

accept(visitor: BaseVisitor)
Parameters
visitor (BaseVisitor)

constructor

constructor(options: Object)
Parameters
options (Object)
Name Description
options.id string The name of the struct.
options.fields Array<Expression> The fields of the struct.

id

The name of the struct.

id

Type: string

fields

The fields of the struct.

fields

Type: Array<Expression>

accept

accept(visitor: BaseVisitor)
Parameters
visitor (BaseVisitor)

Instance

Represents an instance of a class or data structure with associated properties.

new Instance(classInstance: (Struct | ArrayList), properties: (Environment | Array))
Parameters
classInstance ((Struct | ArrayList)) The class instance associated with this Instance .
properties ((Environment | Array)) The properties associated with this Instance .
Instance Members
constructor(options)
id
args
accept(visitor)
set(name, value, location)
get(name, location)

constructor

constructor(options: Object)
Parameters
options (Object)
Name Description
options.object Expression The object to get the property.
options.property string The property to get.

object

The object to get the property.

object

Type: Expression

property

The property to get.

property

Type: string

accept

accept(visitor: BaseVisitor)
Parameters
visitor (BaseVisitor)

constructor

constructor(options: Object)
Parameters
options (Object)
Name Description
options.object Expression The object to set the property.
options.property string The property to set.
options.value Expression The value to set.
options.sig string The assignation of the property.

object

The object to set the property.

object

Type: Expression

property

The property to set.

property

Type: string

value

The value to set.

value

Type: Expression

sig

The assignation of the property.

sig

Type: string

accept

accept(visitor: BaseVisitor)
Parameters
visitor (BaseVisitor)

constructor

constructor(options: Object)
Parameters
options (Object)
Name Description
options.args Array<Expression> The name of the array instance.
options.type string The type of the array instance.
options.dim Array<number> The dimension of the array instance.

args

The name of the array instance.

args

Type: Array<Expression>

type

The type of the array instance.

type

Type: string

dim

The dimension of the array instance.

dim

Type: Array<number>

accept

accept(visitor: BaseVisitor)
Parameters
visitor (BaseVisitor)

BaseVisitor

new BaseVisitor()
Instance Members
visitExpression(node)
visitLiteral(node)
visitGroup(node)
visitVarValue(node)
visitUnary(node)
visitArithmetic(node)
visitRelational(node)
visitLogical(node)
visitTernary(node)
visitVarAssign(node)
visitReturn(node)
visitContinue(node)
visitBreak(node)
visitCase(node)
visitSwitch(node)
visitFor(node)
visitForEach(node)
visitWhile(node)
visitIf(node)
visitBlock(node)
visitPrint(node)
visitExpressionStatement(node)
visitVarDeclaration(node)
visitCallee(node)
visitFuncDeclaration(node)
visitStructDeclaration(node)
visitInstance(node)
visitGet(node)
visitSet(node)
visitArrayInstance(node)

BaseVisitor

Base visitor class.

new BaseVisitor()
Instance Members
visitExpression(node)
visitLiteral(node)
visitGroup(node)
visitVarValue(node)
visitUnary(node)
visitArithmetic(node)
visitRelational(node)
visitLogical(node)
visitTernary(node)
visitVarAssign(node)
visitReturn(node)
visitContinue(node)
visitBreak(node)
visitCase(node)
visitSwitch(node)
visitFor(node)
visitForEach(node)
visitWhile(node)
visitIf(node)
visitBlock(node)
visitPrint(node)
visitExpressionStatement(node)
visitVarDeclaration(node)
visitCallee(node)
visitFuncDeclaration(node)
visitStructDeclaration(node)
visitInstance(node)
visitGet(node)
visitSet(node)
visitArrayInstance(node)

ArithmeticOperation

Performs an arithmetic operation between two literals.

new ArithmeticOperation(op: string, left: Literal, right: Literal, location: Object): Literal
Parameters
op (string) The arithmetic operator ('+', '-', '*', '/', '%').
left (Literal) The left operand of the operation.
right (Literal) The right operand of the operation.
location (Object) The location information for error reporting.
Returns
Literal: The result of the arithmetic operation as a new Literal object.
Throws
  • LocationError: If any of the operands are null, if operand types are invalid, or if an unsupported operator is used.
  • Error: If the operation is not implemented.

createCommonRules

Creates common rules for arithmetic operations between types.

createCommonRules(): Array<Array<string>>
Returns
Array<Array<string>>: A list of rules for type compatibility in arithmetic operations.

createCommonRules

Creates common rules for relational operations between types.

createCommonRules(): Array<Array<string>>
Returns
Array<Array<string>>: A list of rules for type compatibility in relational operations.

OperationRules

A map defining the operation rules for each arithmetic operator.

OperationRules

Type: Object<string, Array<Array<string>>>

OperationRules

A map defining the operation rules for each relational operator.

OperationRules

Type: Object<string, Array<Array<string>>>

Invocable

Represents an invocable entity, which can be invoked with arguments. This is a base class intended to be extended by specific invocable types.

new Invocable()
Instance Members
arity()
invoke(interpreter, args)

LogicalOperation

Performs a logical operation between two literals of type 'bool'.

new LogicalOperation(op: string, left: Literal, right: Literal, location: Object): Literal
Parameters
op (string) The logical operator ('&&' for logical AND, '||' for logical OR).
left (Literal) The left operand of the logical operation, which must be of type 'bool'.
right (Literal) The right operand of the logical operation, which must be of type 'bool'.
location (Object) The location information for error reporting.
Returns
Literal: The result of the logical operation as a new Literal object of type 'bool'.
Throws
  • LocationError: If either operand is null, if operand types are not 'bool', or if an unsupported operator is used.

RelationalOperation

Performs a relational operation between two literals.

new RelationalOperation(op: string, left: Literal, right: Literal, location: Object): Literal
Parameters
op (string) The relational operator ('<', '>', '<=', '>=', '==', '!=').
left (Literal) The left operand of the relational operation.
right (Literal) The right operand of the relational operation.
location (Object) The location information for error reporting.
Returns
Literal: The result of the relational operation as a new Literal object of type 'bool'.
Throws
  • LocationError: If either operand is null, if operand types are invalid for the operation, or if an unsupported operator is used.

UnaryOperation

Performs a unary operation on a single literal.

new UnaryOperation(op: string, ext: Literal, location: Object): Literal
Parameters
op (string) The unary operator ('-' for negation, '!' for logical NOT).
ext (Literal) The literal operand on which the unary operation is performed.
location (Object) The location information for error reporting.
Returns
Literal: The result of the unary operation as a new Literal object.
Throws
  • LocationError: If the operand's value is null, if the operand's type is invalid for the given operator, or if the operator is not supported.

peg$result

peg$result

Type: any

AbstractInstance

Represents an abstract instance that cannot be instantiated directly. This class serves as a base for concrete instances like Struct and ArrayList.

new AbstractInstance(classInstance: (Struct | ArrayList), properties: (Environment | Literal))
Parameters
classInstance ((Struct | ArrayList)) The class instance associated with this abstract instance.
properties ((Environment | Literal)) The properties associated with this instance.
Instance Members
set(name, value, location)
get(name, location)
clone()

ArrayList

Represents an ArrayList, which is an invocable entity capable of creating and managing arrays.

new ArrayList(node: ArrayListInstance, args: Array<any>)

Extends Invocable

Parameters
node (ArrayListInstance) The ArrayListInstance associated with this ArrayList.
args (Array<any>) The arguments provided to this ArrayList.
Instance Members
node
args
arity()
invoke(interpreter, args)
getDefaultValue(type)
createDefaultArray(type, dim)
getArray(interpreter)

cloneLiteral

Clones a Literal object, including its value and type.

cloneLiteral(literal: Literal): Literal
Parameters
literal (Literal) The Literal object to clone.
Returns
Literal: A new Literal object with the cloned value and type.
Throws
  • Error: If the provided argument is not an instance of Literal.

cloneValue

Clones a value, handling instances of Literal, ArrayListInstance, and other types.

cloneValue(value: any): any
Parameters
value (any) The value to clone.
Returns
any: The cloned value.

cloneArrayListInstance

Clones an ArrayListInstance object, including its class instance and properties.

cloneArrayListInstance(arrayListInstance: ArrayListInstance): ArrayListInstance
Parameters
arrayListInstance (ArrayListInstance) The ArrayListInstance object to clone.
Returns
ArrayListInstance: A new ArrayListInstance object with cloned properties.
Throws
  • Error: If the provided argument is not an instance of ArrayListInstance.

NativeFunction

Represents a native function that can be invoked with a specific arity.

new NativeFunction(arity: number, func: Function)

Extends Invocable

Parameters
arity (number) The number of arguments the function expects.
func (Function) The function to execute when invoked.

Natives

A collection of native functions available in the environment.

Natives

Type: Object<string, NativeFunction>

Static Members
parseInt
parsefloat
toString
toLowerCase
toUpperCase
typeof
System.out.println
indexOf
join
Object.keys

OutsiderFunction

Represents a function defined outside the current scope, with its own environment.

new OutsiderFunction(node: FuncDeclaration, closure: Environment)

Extends Invocable

Parameters
node (FuncDeclaration) The function declaration node.
closure (Environment) The environment in which the function was declared.
Instance Members
node
closure
arity()
invoke(interpreter, args)

Struct

Represents a structure (or class) in the system, allowing for the creation of structured instances.

new Struct(node: StructDeclaration, closure: Environment)

Extends Invocable

Parameters
node (StructDeclaration) The structure declaration node.
closure (Environment) The environment in which the structure was declared.
Instance Members
node
closure
arity()
invoke(interpreter, args)

StructInstance

Represents an instance of a structure, holding its properties and allowing access to them.

new StructInstance(classInstance: Struct, environment: Environment)

Extends AbstractInstance

Parameters
classInstance (Struct) The structure class this instance belongs to.
environment (Environment) The environment holding the instance properties.
Instance Members
set(name, value, location)
get(name, location)
clone()

ArrayListInstance

Represents an instance of an ArrayList, holding its elements and allowing access to them.

new ArrayListInstance(classInstance: ArrayList, arrayList: Literal)

Extends AbstractInstance

Parameters
classInstance (ArrayList) The ArrayList class this instance belongs to.
arrayList (Literal) The list of literals representing the array elements.
Instance Members
set(index, value, location)
get(index, location)
clone()

BreakException

Exception used to signal a break in loop control.

new BreakException()

Extends Error

ContinueException

Exception used to signal a continue in loop control.

new ContinueException()

Extends Error

ReturnException

Exception used to signal a return from a function.

new ReturnException(value: Literal)

Extends Error

Parameters
value (Literal) The value to be returned from the function.

VarDeclarationF

Creates a Literal based on the variable declaration type and value.

new VarDeclarationF(type: string, name: string, value: (Literal | null), location: Location): Literal
Parameters
type (string) The type of the variable.
name (string) The name of the variable.
value ((Literal | null)) The initial value of the variable.
location (Location) The location in the source code where the variable is declared.
Returns
Literal: The resulting Literal based on the type and value.
Throws
  • LocationError: If the type is 'var' and no value is provided, or if there is a type mismatch.

track

track(name: string, value: any, scope: string, location: object): (object | null)
Parameters
name (string)
value (any)
scope (string)
location (object)
Returns
(object | null):

isDuplicate

isDuplicate(name: string, scope: string, location: object): boolean
Parameters
name (string)
scope (string)
location (object)
Returns
boolean:

toHtmlTable

toHtmlTable(): string
Returns
string: