The Transy Language Compiler and Executor was created for the Compiler Construction course during the fall of 2017 at Transylvania University
Brendan Thompson
Tags: projects C++
For more information on the Transy Language, see my complete version of the Transy Language Specification.
The compiler itself is a 3-pass compiler. On its first pass it gathers all of the line labels into the LineLabel table. On the second pass it removes all spaces, removes all blank lines, and changes all characters except those in literals to uppercase. It outputs the modified file as noblanks.txt
. This file is what is read in during the third pass. The compiler parses the command names and calls on the specific command handlers to process the actual command.
This is the main driver for the compiler. It takes in the arguments and parses them to get the flags and the filename. Basically, it creates an instance of BRENxCompiler and tells it to prepare for compilation, to compile, and then to shutdown.
If the -x
flag is set, is calls upon the executor after successfully completing compilation
Symbolic Constants
MAX_NUM_FLAGS 7
This is the class defined in Compiler.cpp & Compiler.h.
Preprocessing
Compiling
When Finished Compiling
.core
file.literal
file-x
flag was set and it needs to close the filesSymbolic Constants
MAX_NUM_FLAGS 7
MAX_NUM_LINES_IN_TRANSY_PROGRAM 1000
This is the class defined in FileManager.cpp & FileManager.h.
Preprocessing
.transy
, .obj
, .noblanks
, .core
, .literal
)Compiling
.obj
fileWhen Finished Compiling
.core
file.literal
fileThis is the class defined in SymbolTable.cpp & SymbolTable.h.
Compiling
.obj
fileWhen Finished Compiling
.core
Type Definitions
memoryTableObject
- string name
- unsigned int memoryLocation (which is just the index and therefore unnecessary)
- double value
- bool isConstant (necessary when adding a constant instead of a variable)
- bool isArray (necessary when adding an array)
- unsigned int size (for arrays)
Symbolic Constants
MAX_NUM_VARIABLES 1001
NOT_FOUND_IN_ARRAY -1
UNDEFINED_VALUE 0.123456789
INDEX_COMPILATION_RESULT 1000
SUCCESSFULLY_COMPILED 0
FAILED_COMPILATION 1
This is the compiler’s object defined in lineLabelTable.cpp & lineLabelTable.h.
Preprocessing
Compiling
Type Definitions
lineLabelObject
- string labelName
- int transyLineNumber
- int objLineNumber (gets mapped when syncing during preprocessing)
Symbolic Constants
MAX_NUM_LINES 1001
NOT_FOUND_IN_ARRAY -1
This is the compiler’s object defined in ExpressionFixConverter.cpp & ExpressionFixConverter.h. It handles converting assignment statements from infix to postfix. It relies heavily on the ExpressionConvertingMatrix to determine what actions to take for each input value, given the current state of the expression.
The executor is significantly simpler than the compiler. It assumes that all of the lines of code are correct, and therefore doesn’t handle many errors. It does give a few warnings.
This is the main driver for the executor. It takes in the arguments and parses them to get the flags and the filename. Basically, it creates an instance of BRENxExecutor and tells it to prepare for execution, to execute, and then to shutdown.
Symbolic Constants
MAX_NUM_FLAGS 7
This is the main executor class created in Executor.cpp & Executor.h.
Preparing
Executing
Symbolic Constants
MAX_NUM_FLAGS 7
MAX_NUM_LINES_IN_TRANSY_PROGRAM 1000
The main file manager for the executor
Loads all of files
.obj
into ProgramLineManager.literal
into LiteralManager.core
into MemoryManagerThe main file manager for the executor created in EFileManager.cpp & EFileManager.h
Loads all of files
.obj
into ProgramLineManager.literal
into LiteralManager.core
into MemoryManagerThe main MemoryManager for the executor created in CoreMemory.cpp & CoreMemory.h
Symbolic Constants
MAX_NUM_VARIABLES 1001
MAX_SIZE_TEMP_MEM 20
INDEX_TEMP_MEM (MAX_NUM_VARIABLES - MAX_SIZE_TEMP_MEM - 1)
The main ProgramLineManager for the executor created in ProgramLineTable.cpp & ProgramLineTable.h
Symbolic Constants
MAX_NUM_LINES 1000
MAX_NUM_PARAMETERS 40 (for assignment statements)
The main LoopManager for the executor created in LoopManager.cpp & LoopManager.h
Type Definitions
loopObject
int lineNumber
int runnerAddress
int startIndexAddress
int endIndexAddress
double incrementAmountAddress
Symbolic Constants
MAX_NUM_LOOPS 7
This is the class defined in literalTable.cpp & literalTable.h. It is used during Compilation and Execution
Compiling
When Finished Compiling
.literal
Executing
Type Definitions
literalTableObject (for compilation)
- string variableName;
- string literalString;
- unsigned int memoryLocation (which is just the index and therefore unnecessary)
Symbolic Constants
MAX_NUM_LITERALS 100
NOT_FOUND_IN_ARRAY -1
UNDEFINED_LITERAL 0.123456789
This section is currently the exact same as the Transy Language Specification
Takes in up to 7 variables to be read in to Core Memory
example:
read firstVar, secondVar, thirdVar
Takes in up to 7 variables from Core Memory to be written out to the console
example:
write firstVar, secondVar, thirdVar
Tells the Executor to finish executing
example:
stop
Allocates the space for up to 7 arrays at a time in Core Memory
example:
dim firstArray[10], secondArray[15], thirdArray[20]
Reads in values to the array locations in Core Memory between the specified startIndex and endIndex
example:
aread firstArray, 3, 8
Writes the values to the screen from Core Memory in the array locations between the specified startIndex and endIndex
example:
awrite firstArray, 3, 8
Starts executing at the line specified by the line label
example:
firstLineLabel:
goto firstLineLabel
Loops while the runnerVariable has not yet crossed the endValue. The runnerVariable must be a variable, while the startIndex, endIndex, and incrementAmount can be variables or constants. It is a pre-test loop
The conditional is based off the incrementAmount.
Conditions
<=
>=
!=
example:
loop runnerVariable, startIndex, endIndex, IncrementAmount
a = a + runnerVariable
loop-end
Manipulates the runnderVariable by incrementAmount, based off the last loop encountered by the executor. Then it checks the conditional of that loop. If it succeeds, the program begins executing at the first line in the loop. If it fails, the program continues executing at the next line after loop-end
example:
loop runnerVariable, startIndex, endIndex, IncrementAmount
a = a + runnerVariable
loop-end
Manipulates the runnderVariable by incrementAmount, based off the last loop encountered by the executor. Then it checks the conditional of that loop. If it succeeds, the program begins executing at the first line in the loop. If it fails, the program continues executing at the next line after loop-end
example:
loop runnerVariable, startIndex, endIndex, IncrementAmount
a = a + runnerVariable
loop-end
Jumps to one of the line labels specified based on whether the supplied value is negative, zero, or positive
example:
ifa (varToTest) negativeLabel, zeroLabel, positiveLabel
negativeLabel:
lwrite "The value was negative\n"
goto finishedLabel
zeroLabel:
lwrite "The value was zero\n"
goto finishedLabel
positiveLabel:
lwrite "The value was positive\n"
finishedLabel: stop
Does not do anything
example:
nop
Prints out the object code for the current program
example:
listo
Reads in a $literalVariable to the Literal Table
example:
lread $literalVariable
Writes the literal to the console. Can take in a $literalVariable or a “literal string”. \n
prints a new line
example:
lwrite "Please supply a literal"
lread $literalVariable
lwrite $literalVariable
Starts executing at the line location specified if the conditional evaluates to true. Otherwise, it continues executing at the next line.
Operators:
<
<=
=
>
>=
!
example:
if (firstVar < secondVar) then firstLineLabel
a = firstVar
goto finishLabel
firstLineLabel: a = secondVar
finishLabel: stop
Clears the screen
example:
cls
Prints all of the values in Core Memory between the startIndex and the endIndex
example:
cdump 0, 10
Executes the mathematical command represented by the operation and given the ID. It then sets the value to the supplied variable
Operations:
sin
= sets the variable to the sin of the IDcos
= sets the variable to the cosin of the IDexp
= sets the variable to the base-e exponential function of the IDabs
= sets the variable to absolute value of the IDalg
= sets the variable to the log base 2 of the IDaln
= sets the variable to the natural log of the IDlog
= sets the variable to the log base 10 of the IDsqr
= sets the variable to the square root of the IDexample:
subp sin(destinationVariable, sourceValue)
All basic mathematical expressions can be handled.
Operators:
+
-
*
/
^
()
= enforced order of operations[]
= direct access into an array indexexample:
a[2^5] = 0 + 2 / firstArray[index] * (-10 - b ^ 2)