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)]
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))
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
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)))
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))
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)
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.'))
def add_text(text, code, type=None): scope.add_definition(Definition(string_to_signature(text), code, type))
def primitive_integer(sentence): value = int(sentence[0].text) return Definition(sentence, value, IntegerType())
def type_def(text, code, type=_type_type): return Definition(string_to_signature(text), code, type)