Esempio n. 1
0
    def start(self):
        # print(self.stmts().stmt1.block)
        s = self.stmts()
        # begin = s.newlabel()
        # after = s.newlabel()
        # s.emitlabel(begin)
        # s.gen(begin, after)
        # s.emitlabel(after)

        # print(self.top.__dict__)
        # Print tree
        print('\nTREE')
        print(str(s.__dict__))

        compiler = Compiler(self.top, s)
        compiler.compile()
Esempio n. 2
0
def main():
    compiler = Compiler()
    compiler.compile(code)

    print("-------- Original ---------")
    print(compiler.lang_ast())

    print("\n-------- __main__ ---------")
    print(compiler.c_ast())

    with open("test.cc", "w") as f:
        f.write(str(compiler.c_ast()))

    for fname in compiler.generated_files():
        print("\n-------- {} ---------".format(fname))
        with open(fname, "r") as f:
            print(f.read())
Esempio n. 3
0
def run_code(text):
    """text should be a valid c program, code runner will compile and run the program and return its output

    run code will throw any exceptions thrown by the compilation attempt if it fails
    The code will receive no input for the run and should output to std.out"""
    lex = Lexer(text)
    pars = Parser(lex)
    comp = Compiler(pars)
    asm_code = comp.compile()
    directory_path = os.path.dirname(os.path.realpath(__file__))
    os.chdir(directory_path)
    asm_file_name = 'temp_asm_file.asm'
    obj_file_name = 'temp_asm_file.obj'
    executable_file_name = "output_executable.exe"

    asm_file = open(asm_file_name, 'w')
    asm_file.write(asm_code)
    asm_file.close()

    p = subprocess.Popen(["nasm", "-fwin32", asm_file_name],
                         stdout=subprocess.PIPE)
    out, err = p.communicate()
    time.sleep(.5)
    p = subprocess.Popen([
        "gcc", "-m32", obj_file_name, "-o",
        os.path.join(directory_path, executable_file_name)
    ],
                         stdout=subprocess.PIPE)
    out, err = p.communicate()
    time.sleep(.5)
    p = subprocess.Popen([os.path.join(directory_path, executable_file_name)],
                         stdout=subprocess.PIPE)
    out, err = p.communicate()
    time.sleep(.5)
    out = out.decode("utf-8")
    return out
Esempio n. 4
0
from colorama import Back

from compiler.compiler import Compiler
from runtime.runtime import Runtime

source = open("sample1.aspl")
source = source.read()

print("SOURCE IS: ", source)

compiler = Compiler()
code = compiler.compile(source)

print(Back.RED)

for line in code:
    print(line)
print(Back.RESET)

runtime = Runtime()
runtime.run(code)
Esempio n. 5
0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import sys
sys.path.append(os.path.abspath('../..'))

from compiler.compiler import Compiler
from compiler.backends import *
from compiler.utility import circuit_drawer, save_results

compiler = Compiler(get_coupling(qx5))
# It's possible to compile and run on a simulator instead of a real backend, if you want
cobj = compiler.compile(16, qx5, algo='envariance')
# To draw circuit without running it, uncomment next line and comment the others
# circuit_drawer(cobj['circuit'], filename='envariance')
robj = compiler.run(cobj, backend=qx5)
circuit_drawer(robj['ran_qasm'], filename='envariance')
results = robj['results']
save_results(results, 'envariance.txt', directory='Data/envariance')
Esempio n. 6
0
"""Module that runs self written 'C' language compiler for file 'source.c'

This compiler is written using 'rply' project 'https://github.com/alex/rply'
For proper work of my compiler you need to install 'rply' from github manually
because on PYPI there is older version.
"""

from compiler.compiler import Compiler
from config import PATH_TO_SOURCE_FILE, PATH_TO_OUTPUT_FILE

cmp = Compiler(PATH_TO_SOURCE_FILE, PATH_TO_OUTPUT_FILE)
cmp.compile()
cmp.print_abstract_syntax_tree()
input("\nProgram has finished. To exit press <Enter>\n")
Esempio n. 7
0
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import sys
sys.path.append(os.path.abspath('../..'))

from compiler.compiler import Compiler
from compiler.backends import *
from compiler.utility import circuit_drawer, save_results

logging.getLogger('compiler.compiler').setLevel(logging.DEBUG)

compiler = Compiler(get_coupling(qx5))
# It's possible to compile and run on a simulator instead of a real backend, if you want
cobj = compiler.compile(16, qx5)
# To draw circuit without running it, uncomment next line and comment the others
# circuit_drawer(cobj['circuit'], filename='ghz')
robj = compiler.run(cobj, backend=qx5, shots=10)
circuit_drawer(robj['ran_qasm'], filename='ghz')
results = robj['results']
save_results(results, 'ghz.txt', directory='Data/ghz/')
Esempio n. 8
0
from ast.visualizer import Visualizer
from parser_ import Parser
from lexer import Lexer
from compiler.compiler import Compiler

code = """
void main(){
    if(4 >= 3){printf("Hurray");}
}
"""

lex = Lexer(text=code)
lex2 = Lexer(text=code)
parser = Parser(lexer=lex)
parser2 = Parser(lexer=lex2)
comp = Compiler(parser=parser)

viz = Visualizer(parser=parser2)


print(comp.compile())
viz.visualize()


Esempio n. 9
0
                temp = cur;
                cur = prev + cur;
                prev = temp;
                counter = counter + 1;
            }
            printf("%d  ", cur);
            x = x +1;
        }
    }
"""

from tests.system_tests.code_runner import run_code
from lexer import Lexer
from parser_ import Parser
from compiler.compiler import Compiler
from ast.visualizer import Visualizer

l = Lexer(code2)
p = Parser(l)
c = Compiler(p)
print("=" * 10)
print(c.compile())
print("=" * 10)

l = Lexer(code2)
p = Parser(l)
v = Visualizer(p)

print(run_code(code2))
v.visualize()
Esempio n. 10
0
import os
import sys
sys.path.append(os.path.abspath('../..'))

from compiler.compiler import Compiler
from compiler.backends import *
from compiler.utility import circuit_drawer, save_results

logging.getLogger('compiler.compiler').setLevel(logging.DEBUG)

compiler = Compiler(get_coupling(qx5))
# Oracle can be specified by alias:
#               '00' equals a '00..0000' oracle
#               '10' equals a '10..1010' oracle
#               '11' equals a '11..1111' oracle
#
# Remember that if the backend has 16 qubit, parity will run on 15 qubits because one is used as ancilla
cobj = compiler.compile(15, qx5, algo='parity', oracle='10')
# To draw circuit without running it, uncomment next line and comment the others
# circuit_drawer(cobj['circuit'], filename='parity')
robj = compiler.run(cobj, backend=qx5)
circuit_drawer(robj['ran_qasm'], filename='parity_10')
results = robj['results']
save_results(results, 'parity_10.txt', directory='Data/parity_10/')

# Oracle can also be explicitly set if custom_mode is True
cobj = compiler.compile(15, qx5, algo='parity', oracle='101101011010011', custom_mode=True)
circuit_drawer(cobj['qasm'], filename='parity_10')
robj = compiler.run(cobj, backend=online_sim)