Exemplo n.º 1
0
def interpret(s):
	tm = ['not','true','false','+', '*', '(', ')', 'log', 'print', 'assign', 'if', 'while', ';', ':=', '{', '}', 'xor']
	ts = tokenize(tm,s)
	parse = program(ts)[0]
	if parse is not None:
		exp = execProgram({}, parse)[1]
		if exp is not None:
			return exp
Exemplo n.º 2
0
def interpret(syntax):
	terminals = ['print','assign', 'if', 'do','until', 'true', 'false', 'not', 
				'nonzero', 'and', '*', '+', '(', ')',',',':=','{','}', ';']
	tokens = tokenize(terminals, syntax)
	parseTree = parse.program(tokens)
	if (parseTree != None):
		(env, e) = execProgram({}, parseTree[0])
		return e
	else:
		return None	
Exemplo n.º 3
0
def interpret(s):
    tokens = tokenize(['true', 'false','(',')','not','xor','log', '+','*','print','assign', 'if', 'while',';', ':=','{','}','==','<'], s)
    if not tokens is None:
        r = parse.program(tokens)
        if not r is None:
            (parsedTokens,tmp) =r
            r = execProgram({}, parsedTokens)
            if not r is None:
                (env, output)= r
                return output
Exemplo n.º 4
0
def interpret(s):
    tm = [
        'not', 'true', 'false', '+', '*', '(', ')', 'log', 'print', 'assign',
        'if', 'while', ';', ':=', '{', '}', 'xor'
    ]
    ts = tokenize(tm, s)
    parse = program(ts)[0]
    if parse is not None:
        exp = execProgram({}, parse)[1]
        if exp is not None:
            return exp
Exemplo n.º 5
0
def interpret( str ):
    r = parse.program( parse.tokenize( str ))
    if r is None:
        return None
    (  e, tokens ) = r
    if len( tokens ) > 0:
        return None
    accum = set()
    accumVariablesInProgram( accum, e )
    print( "accum = ", accum )
    env = { STDOUT: ""}
    r = execProgram( env, e )
    return env[ STDOUT ]
Exemplo n.º 6
0
def interpret(tokens):
        regex =['print' , 'assign' , ':=' , 'if', 'true' , 'false' , 'while' , 'not' , 'xor' , '+'\
                , '*' , ';' , '{' , '}' , '(' , ')' , ' ']
        tokens = parse.tokenize(regex, tokens)
        tokens = parse.program(tokens)
        return execProgram({}, tokens[0])[1]
Exemplo n.º 7
0
import parse,interpret


print(parse.program(['if', 'true', '{', 'print', '1', ';', '}']))
print(parse.number('10', False))
print(interpret.interpret('assign x := true; assign y := true; assign z:= 10; assign a := -1; while x==y{ if z<1{assign y := false;} print z; assign z:= z + a ;} print x; print y;'))
print(interpret.interpret('assign x:= 0; while not(x < 1){print x; assign x:= x+0;} print false; '))
print(interpret.interpret('print 3 == 3;'))
Exemplo n.º 8
0
def mannc(filename):

    #
    # トークナイズ
    #
    mytokenize.mytokenize(filename)

    print('#PRINT TKN START')
    for t in asmd.tkn:
        t.myself()
    print('#PRINT TKN END')

    #
    # パース
    #
    asmd.offset = 0
    parse.program()

    print('#PRINT glvars_t START')
    for g in asmd.glvars_t.keys():
        print('#name={0}'.format(g))
        asmd.glvars_t[g].myself()
    print('#PRINT glvars_t END')

    print('#offset {0}'.format(asmd.offset))

    #
    # ローカル変数のoffsetをセットする リファクタリング用
    #
    for index1 in range(len(asmd.code)):
        if asmd.code[index1].kind == ND.FUNCDEF:
            # 関数にパラメータがある場合だと
            # offsetの初期値は 56
            # そうでない場合は 0
            offset = 0
            for index2 in asmd.code[index1].lvars2.keys():
                offset = asmd.align_to( offset, \
                     asmd.code[index1].lvars2[index2].ty.align)
                offset += asmd.code[index1].lvars2[index2].ty.size
                asmd.code[index1].lvars2[index2].offset = offset
                print("#offset {0} = {1}".format(
                    index2, asmd.code[index1].lvars2[index2].offset))

            asmd.code[index1].offset = offset
            print("#func offset={0}".format(offset))

    #
    # アセンブリコード出力開始
    #
    # ヘッダ
    print('.intel_syntax noprefix')

    # グローバル変数のコードを出力する
    gen.gen_gvar()

    print('.text')
    print('.global main')

    # コード生成
    for nd in asmd.code:

        asmd.offset = nd.offset
        asmd.lvars_t = nd.lvars_t
        asmd.lvars = nd.lvars

        # 1stmt毎にループ
        gen.gen(nd)
        # 1stmtはスタックに値を残すので rax に pop する
        #print('\tpop rax')

    #エピローグ
    #最後の式の結果がRAXに残っているのでそれが返り値になる
    #print('\tmov rsp, rbp')
    #print('\tpop rbp')

    print("\tret")
Exemplo n.º 9
0
def testProgram( str ):
    r = parse.program( parse.tokenize(  str ) )
    print( "program( " + str + " ) ----> " , r )