예제 #1
0
def make_test_scopes():
    scope0 = Scope(None)
    scope0.add_definition(
        Definition(string_to_signature('Fake sentence for testing.'), 0))
    scope0.add_definition(
        Definition(string_to_signature('Beginning Middle. end.'), 1))
    return [scope0, Scope(scope0)]
예제 #2
0
def define_function(scope, head, body):
    """Create a new function Definition. 'Define Head. to be Body. .'

    :param scope: The enclosing scope around the definition.
    :param head: The Sentence that defines the function signature.
    :param body: The Sentence that defines the function body."""
    params = []
    for item in head.iter_sub():
        params.append(item)

    if 0 == len(params):
        return AddDefAction(Definition(head, body))

    def eval_function(scope, *args):
        if len(args) != len(params):
            raise LSRunningError('Incorrect number of arguments. expected: ' +
                                 str(len(params)) + ' actual: ' +
                                 str(len(args)))
        local_scope = Scope(scope)
        for (param, arg) in zip(params, args):
            new_def = Definition(param, arg)
            local_scope.add_definition(new_def)
        # Head should be defined in the parent scope.
        return evaluate(body, local_scope)

    ftype = FunctionType([anything_type] * len(params), anything_type)

    return AddDefAction(Definition(head, eval_function, ftype))
예제 #3
0
 def make_test_scope(self):
     scope = Scope()
     scope.add_definition(Definition(string_to_signature('Unit.'), 0))
     scope.add_definition(Definition(
         string_to_signature('Something with Sub sentence. to parse.'), 1))
     scope.add_definition(Definition(
         string_to_signature('Define New thing. to be a new type.'), 2))
     return scope
예제 #4
0
def define_constant(scope, head, body):
    """Define a new immutable value.

    Currently values may not have fields."""
    for _ in head.iter_sub():
        raise Exception('define_constant: Does not accept fields.')
    return AddDefAction(Definition(head, evaluate(body, scope)))
예제 #5
0
 def test_match_sentence(self):
     pattern = [FirstToken('Test'), WordToken('value')]
     to_match = Sentence(pattern + [PeriodToken()])
     code = None
     test_def = Definition(pattern, code)
     scope = Scope(None)
     scope.add_definition(test_def)
     self.assertIs(test_def, scope.match_sentence(to_match))
예제 #6
0
 def eval_function(scope, *args):
     if len(args) != len(params):
         raise LSRunningError('Incorrect number of arguments. expected: ' +
                              str(len(params)) + ' actual: ' +
                              str(len(args)))
     local_scope = Scope(scope)
     for (param, arg) in zip(params, args):
         new_def = Definition(param, arg)
         local_scope.add_definition(new_def)
     # Head should be defined in the parent scope.
     return evaluate(body, local_scope)
예제 #7
0
 def test_new_define_scope(self):
     outer_scope = Scope()
     outer_scope.add_definition(
         Definition(string_to_signature('Define Head. to be a built in.'),
                    None))
     inner_scope = outer_scope.new_define_scope(
         string_to_signature(
             'Check that The Sentences. given. are Defined. .'))
     # raises on missing sentence.
     inner_scope.match_sentence(
         string_to_signature('Check that Things. are Something. .'))
     inner_scope.match_sentence(string_to_signature('The Things. given.'))
     inner_scope.match_sentence(string_to_signature('Defined.'))
     with self.assertRaises(NoDefinitionError):
         inner_scope.match_sentence(string_to_signature('Sentences.'))
예제 #8
0
 def add_text(text, code, type=None):
     scope.add_definition(Definition(string_to_signature(text), code, type))
예제 #9
0
def primitive_integer(sentence):
    value = int(sentence[0].text)
    return Definition(sentence, value, IntegerType())
예제 #10
0
def type_def(text, code, type=_type_type):
    return Definition(string_to_signature(text), code, type)