Exemplo n.º 1
0
def test_parser():
    from lex import Tokenizer, mockfile

    test_list = """
# NEXT x;
# NEXT x.y;
# NEXT (x=1);
# NEXT x.y[5][{ inc bob; }, "hello"](x=6);
# NEXT (a=1, b=2)(x=a);
# NEXT ![x] { 4;};
# NEXT ![x, y] { x;};
""".split("# NEXT")

    print "-"*50
    for inputString in test_list:

        tokenizer = Tokenizer(mockfile(inputString), False)
        tokenList = tokenizer.read_all()
        tokenString = tokenizer.as_string()

        parser = Parser(tokenList)
        code = parser.read_all()
        codeString = parser.as_string()
        
        print inputString.strip()
        print tokenString
        print codeString
        print "-"*50
Exemplo n.º 2
0
def main(fileObj):
    global_env = make_builtins()
    tokenizer = Tokenizer(PeekableStream(fileObj), False)
    tokenList = tokenizer.read_all()
    tokenString = tokenizer.as_string()
    # print tokenString

    parser = Parser(tokenList)
    code = parser.read_all()
    codeString = parser.as_string()
    # print codeString
        
    env = Dict(global_env)
    val = code.evaluate(env)
    result = val.call(None, List([]), env)

    if result is not None:
        print result.as_string("")
    else:
        print None
    print "-"*50
Exemplo n.º 3
0
def test_evaluator():
    from parse import Parser
    from lex import Tokenizer, mockfile

    from function import make_builtins

    global_env = make_builtins()

    test_list = """

# NEXT 4;
# NEXT def x = 4;
# NEXT def x = 4; x;
# NEXT def x = ["a", "b"]; x;
# NEXT def x = ["a", "b"]; x[0];
# NEXT ["a", "b"][0];
# NEXT {4;}[];
# NEXT ![x]{x;};
# NEXT ![x]{x;}[5];
# NEXT 
def x = 6;
def ret4 = ![x]{
  set x = 4;
  x;
};
set x = 9;
ret4[x];
x;
# NEXT 
def a = 1;
def b = ();
def c = ( d=3);
# NEXT 4.str;
# NEXT 4.str[];
# NEXT 4.add[2];

# NEXT 

def add4 = {
  ![y]{4.add[y]}
};
add4[][1];

# NEXT 

def addN = ![n]{
  ![y]{n.add[y]}
};
var add2 = addN[2];

add2[9];

# NEXT 

true;

# NEXT 

true.ifTrue[{4},{5}];

# NEXT 

false.ifTrue[{4},{5}];


# NEXT File;
# NEXT File["../README.md"];
# NEXT 

def x = File["../README.md"]; 
x.read[1];
var y = x.read[5];
x.close[];
y;

# NEXT 

def x = ( y=4 );
def n = { inc x; y };
n[];

# NEXT 

def x = {
  def y = 4;
  Frame[];
};

x[];


# NEXT 


def Statement = {
  var kind;
  def str  = {Error["not implemented"]};
  def call = {Error["not implemented"]};
  def eval = {Error["not implemented"]};
  def get  = {Error["not implemented"]};
  Frame[];
}[];

Print["Statement = "];

Print[Statement];

def Var = {
  inc Statement;
  set kind = "var";
  var name;
  var chain;

  Frame[];
}[];

Print["Var = "];
Print[Var];

4;


""".split("# NEXT")

    print "-"*50
    for inputString in test_list:
        print inputString.strip()

        tokenizer = Tokenizer(mockfile(inputString), False)
        tokenList = tokenizer.read_all()
        tokenString = tokenizer.as_string()
        print tokenString

        parser = Parser(tokenList)
        code = parser.read_all()
        codeString = parser.as_string()
        print codeString

        env = Dict(global_env)
        val = code.evaluate(env)
        result = val.call(None, List([]), env)

        if result is not None:
            print result.as_string("")
        else:
            print None
        print "-"*50