Beispiel #1
0
    def assignment_stmt(self, p):
        # ID type and existence checking
        if is_in_dict(self.labelsTable, p[0].val):
            idtype = self.labelsTable[p[0].val].type
            idvar = self.labelsTable[p[0].val]
            if idtype == Constants.FLOAT_TYPE:
                if p[2].type != Constants.FLOAT_TYPE:
                    newvar = self.new_var()
                    self.gen('RTOI ' + newvar + ' ' + p[2].result)
                    p[2].type = Constants.FLOAT_TYPE
                    p[2].result = newvar
            else:
                if p[2].type != idtype:
                    eprint(str(p.lineno) + ' : cant cast float to int')
                    self.errors += 1
                    return
        # writing code
            if idtype == Constants.FLOAT_TYPE:
                command = 'RASN'
            else:
                command = 'IASN'

            self.gen(command + ' ' + str(idvar.result) + ' ' +
                     str(p[2].result))
        else:
            eprint(
                str(p.lineno) + ' : cant resolve identifier "' +
                str(p[0].val) + '"')
            self.errors += 1
            return
        return p[0]
Beispiel #2
0
 def break_stmt(self, p):
     if len(self.switchStack) < 1:
         eprint(str(p.lineno) + ' : Break outside a switch statement')
         self.errors += 1
     else:  # look whats current switch address
         self.gen('JUMP ' +
                  self.switchStack[len(self.switchStack) - 1]['address'])
     return
Beispiel #3
0
 def factor(self, p):
     if is_in_dict(self.labelsTable, p[0].val):
         idvar = self.labelsTable[p[0].val]
     else:
         idtype = Constants.UNKNOWN_TYPE
         eprint(
             str(p.lineno) + ' : cant resolve identifier "' + p[0].val +
             '"')
         self.errors += 1
         return
     return idvar
Beispiel #4
0
 def idlist(self, p):
     if p[2] in p[0] or is_in_dict(
             self.labelsTable, p[2].val
     ):  # if ID is already declared in idlist or labelstable
         eprint(
             str(p.lineno) + ' : identifier "' + p[2].val +
             '" is declared too many times')
         self.errors += 1
     else:  # else adds ID to idlist
         p[2].type = Constants.UNKNOWN_TYPE
         p[0].append(p[2])
     return p[0]
Beispiel #5
0
    def start_switch(self, p):
        if p[2].type != Constants.INT_TYPE:
            eprint(str(p.lineno) + ' : Cant switch on a float type expression')
            self.errors += 1
            return

        breakAddress = self.new_address()
        self.switchStack.append({
            'address': breakAddress,
            'expResult': p[2].result
        })

        return breakAddress
Beispiel #6
0
    def program(self, p):
        if self.errors > 0:  # if any errors
            eprint(str(self.errors) + ' errors detected')
        else:
            # replacing the addresses symbols with actual line numbers
            for key in self.addressTable:
                self.compiledString = self.compiledString.replace(
                    key, str(self.addressTable[key]))
            self.gen('HALT')
            self.gen('Name: Amit Moyal, Id: 206086365')
            eprint('file compiled successfully')
            return self.compiledString

        return Constants.FAIL  # if there is any errors
Beispiel #7
0
    def input_stmt(self, p):
        if is_in_dict(self.labelsTable, p[2].val):
            idVar = self.labelsTable[p[2].val]

            # finding out the right command
            if self.labelsTable[p[2].val].type == Constants.FLOAT_TYPE:
                command = 'RINP'
            else:
                command = 'IINP'

            self.gen(command + ' ' + idVar.result)

        else:  # ID not found on labelsTable
            eprint(str(p.lineno) + ' : cant resolve identifier "' + p[0] + '"')
            self.errors += 1
        return p[2]
Beispiel #8
0
import sys
from sly import Lexer, Parser
from CpqLexer import CpqLexer
from CpqParser import CpqParser
from Utilities import Constants, eprint

if len(sys.argv) < 2:
    eprint('Not enough arguments')
    exit(0)

fullInputFileName = sys.argv[1]
expectedEnding = '.' + Constants.INPUT_FILE_ENDING

if not fullInputFileName.endswith(expectedEnding):
    eprint('File should end with ' + expectedEnding)
    exit(0)

inputFileName = fullInputFileName.replace(expectedEnding, '')

try:
    cplString = open(fullInputFileName, 'r').read()
except Exception as e:
    eprint('could\'nt find or open the file ' + fullInputFileName + '\n' + str(e))
    sys.exit(0)

try:
    lexer = CpqLexer()
    parser = CpqParser()
    a = parser.parse(lexer.tokenize(cplString))
except:
    eprint('Compiling failed')
Beispiel #9
0
# Main Runner Code from here to end

cwd = sys.path[0]

outputPath = os.path.normpath(
    cwd + "/" + SharedCfg.OUTPUT_FOLDER_NAME)  # Get the output folder's path

if SharedCfg.DEBUG_MODE:  # Debug mode
    os.rmdir(outputPath)  # Reset output (remove output folder)

# Create the output folder or exit if it exists
try:
    os.mkdir(outputPath)  # Create the output folder
except FileExistsError:
    eprint("Error: \"",
           outputPath,
           "\" already exists.\nPlease remove or rename the existing folder.",
           sep="")
    exit(
        73
    )  # Allegedly 73 is EX_CANTCREAT, which seems like it would be the closest

# Main Loop
main_loop_contents()  # Run once with no starting delay

# main_loop_contents() was just run once! Since it wasn't used before now, there was never a need to declare it as 0
loopCount = 1

while MonitorCfg.LOOP_TIMES == 0 or loopCount < MonitorCfg.LOOP_TIMES:  # Left side evaluates first in Python
    # Waiting "before" (to avoid trailing sleep when finished) is possible because main_loop_contents() was run earlier
    time.sleep(MonitorCfg.LOOP_WAIT)