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 test_update_parsed(self): scope = Scope() phrase = 'changeable value is `changeable`' phrase = Phrase(phrase, scope) phrase._update_parsed() self.assertEqual(phrase.latest, "changeable value is True") # no exception scope.change() phrase._update_parsed() self.assertEqual(phrase.latest, "changeable value is False") # no exception scope.change() scope.raise_error() self.assertRaises(KeyError, phrase._update_parsed) # also test not changed, with exception scope = Scope() phrase = 'changeable value is `changeable`' phrase = Phrase(phrase, scope) phrase._update_parsed() self.assertEqual(phrase.latest, "changeable value is True") scope.raise_error() self.assertRaises(KeyError, phrase._update_parsed)
def eval(self, s: Scope) -> Value: env: Scope = Scope(s) ext: Scope = Scope(s) for i, _ in enumerate(self.patterns): binder.define(ext, self.patterns[i].identifier, self.exprs[i].eval(env)) return self.body.eval(ext)
def eval(self, s: Scope) -> Value: env: Scope = Scope(s) ext: List[Scope] for i in range(len(self.patterns)): ext.append(Scope(env)) binder.define(ext[i], self.patterns[i].identifier, self.exprs[i].eval(env)) for x in ext: env.put_all(x) return self.body.eval(env)
def test_compare(self): input_string = "my name is John" input_parsed = link_parser.parse(input_string) phrase = 'my name is `name~Mark`' phrase = Phrase(phrase, Scope()) self.assertEqual(phrase.compare(input_parsed), 0.8) #4/5 simlar links phrase = 'hello' phrase = Phrase(phrase, Scope()) self.assertEqual(phrase.compare(input_parsed), 0)
def test_erase_routine_requests(self): phrase = self.fake_class('Hello world `my_var:32` `name~John`', Scope()) Phrase._erase_routine_requests(phrase) self.assertEqual(phrase.phrase, 'Hello world `my_var:32` `name~John`') phrase = self.fake_class( 'Hello world `routine1<"stop"` `my_var:32` `routine1<"stop"` `name~John`', Scope()) Phrase._erase_routine_requests(phrase) self.assertEqual(phrase.phrase, 'Hello world `my_var:32` `name~John`')
def test_parse(self): phrase = 'Hello `my_var` `my_var:32` `name~John` `routine1<"stop"`' phrase = Phrase(phrase, Scope()) self.assertEqual(phrase.latest, 'Hello value John ') self.assertEqual(phrase.phrase, 'Hello `my_var` John ') self.assertEqual(phrase.substitute, [['my_var', 6, 14]]) self.assertEqual(len(phrase.setters), 1) self.assertEqual(phrase.flexibles, [['name', 'John', 16, 20, None]]) self.assertEqual(len(phrase.requests), 1) # exception raised by Scope module phrase = 'Hello `undefined_var`' phrase = Phrase(phrase, Scope()) self.assertEqual(phrase.latest, None)
def test_erase_flexible_setters(self): phrase = self.fake_class('Hello world `my_var:32` `routine1<"stop"`', Scope()) Phrase._erase_flexible_setters(phrase) self.assertEqual(phrase.phrase, 'Hello world `my_var:32` `routine1<"stop"`') phrase = self.fake_class( 'Hello world `name~John` `my_var:32` `name~Mike` `routine1<"stop"`', Scope()) Phrase._erase_flexible_setters(phrase) self.assertEqual( phrase.phrase, 'Hello world John `my_var:32` Mike `routine1<"stop"`')
def __init__(self, path, default_chains=_default_chains): self.scope = YipScope() self.path = path self.tree = YipLoader.load(open(path)) self.tables = {} self.chains = Scope({c: None for c in _default_chains}) self.built = False
def eval(self, env: Scope) -> Value: e: Scope = env for i, _ in enumerate(self.patterns): e = Scope(e) binder.define(e, self.patterns[i].identifier, self.exprs[i].eval(e)) return self.body.eval(e)
def test_evaluate(self): phrase = 'no substitutions' phrase = Phrase(phrase, Scope()) self.assertEqual(phrase.evaluate(), "no substitutions") phrase = 'iteratable value is `iteratable`' phrase = Phrase(phrase, Scope()) self.assertEqual(phrase.evaluate(), "iteratable value is 2") # 1 is passed by init call self.assertEqual(phrase.evaluate(), "iteratable value is 3") self.assertEqual(phrase.evaluate(), "iteratable value is 4") phrase = 'undefined value is `undefined_var`' phrase = Phrase(phrase, Scope()) self.assertRaises(KeyError, phrase.evaluate) # predicted behavior
def __init__(self): scope = Scope() global_ = Node(None, 'Global') global_.children['files'] = [] global_.scope = scope self.global_ = global_ self.defineNativeTypes()
def lex(fname): source = open(fname, "r").read().rstrip("\r\n") lexer = Lexer(fname, source) tokens, error = lexer.make_tokens() if error: return None, error parser = Parser(tokens) tree = parser.parse() if tree.error: return None, tree.error global_scope = Scope() global_scope.set("NULL", Number(0)) global_scope.set("TRUE", Number(1)) global_scope.set("FALSE", Number(0)) interpreter = Interpreter() context = Context("<main>") context.scope = global_scope result = interpreter.visit(tree.node, context) return result.value, result.error
def resolveFunction(node: Node, parentScope: Scope): for child in node.getChildNodes(): if child.nodeType == NodeType.FUNCTION: child.parentScope = parentScope name = child.getChild('name') if parentScope.classType: if name != '__init__': name = parentScope.classType.name + '__' + name else: name = parentScope.classType.name + name type_ = getFunctionReturnType(child) symbol = Symbol(name, type_) child.symbol = symbol child.resolvedType = type_ global_ = parentScope while global_.parent != None: global_ = global_.parent global_.define(symbol) scope = Scope(parentScope) scope.isFunction = True scope.classType = parentScope.classType child.scope = scope
def test_erase_fixed_setters(self): # origin = 'Hello world `my_var:32` `name~John` `routine1<"stop"`' phrase = self.fake_class('Hello world `name~John` `routine1<"stop"`', Scope()) Phrase._erase_fixed_setters(phrase) self.assertEqual(phrase.phrase, 'Hello world `name~John` `routine1<"stop"`') phrase = self.fake_class( 'Hello world `my_var:32` `name~John` `my_var:32` `routine1<"stop"`', Scope()) Phrase._erase_fixed_setters(phrase) self.assertEqual(phrase.phrase, 'Hello world `name~John` `routine1<"stop"`')
def renderResponseForQuery(self): # constructs a response based on the user's query intent self.__response = "Sorry, I didn't understand that" # (1) initialize default response message self.__scope = Scope(self.__db_handler.checkCurrentScope(self.__activity.getConversationID())) # (2) init scope # (3) Check for a CLARIFICATION object: clarification = self.__db_handler.getCacheForClarification(self.__activity.getConversationID()) if clarification is not None: # clarification exists! entity_type = clarification[2] # check what entity type we SHOULD be getting if len(self.findMatchingEntity(of_type=entity_type)) > 0: # check that we received at least 1 such entity self.__topIntent = Intent(clarification[0]) # recreate old top scoring intent & overwrite updated_e = [Entity(e) for e in clarification[1]] # create old entities for e in self.__entities: # add the NEW entities to the END of the existing list (*to preserve order!*) updated_e.append(e) self.__entities = updated_e # overwrite the entities object self.__is_clarification = True # set indicator (needed for findObject logic) # *If no entities of specified type are found, simply treat query normally & ignore clarification!* e = self.findMatchingEntity("query") # *(4) check for a QUERY entity AFTER the clarification!* query_word = e[0] if len(e) > 0 else "" # store FIRST query that is found (b/c entities are sent IN ORDER) # Non-Historical Intents: if self.__topIntent.intent == "None": # receptacle for unwanted questions *** pass elif self.__topIntent.intent == "Greeting": name = self.__activity.getUserName() # check if user's name is defined self.__response = "Hello, {}".format(name[1]) if name else "Hello" elif self.__topIntent.intent == "GetName": # asking for name self.__response = "I am the Diagnostic Bot" # Recognizer Intents: # (LAST) Persist the scope object & then render the bot's response: self.__db_handler.persistCurrentScope(self.__activity.getConversationID(), self.__scope.getScopeForDB()) self.__activity.sendTextMessage(text="{}".format(self.__response)) # send msg
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 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 transform_v_ident(self, scope): if not self.in_region: return scope if self.is_const(scope): value = scope.get_classical_value(scope.name) s = Scope(scope.line, scope.column, scope_payload=UIntPayload(value), super_scope=scope.super_scope) return s else: return scope
def test_accept(self): scope = Scope() phrase = '`var:13` my name is `name~Jack` `surname~Daniels` `strvar:"string"` `routine<1` `routine2<"ok"`' phrase = Phrase(phrase, scope) phrase.accept('my name is John Walker') self.assertEqual(scope.toset, { 'var': 13, 'name': 'example', 'surname': 'example', 'strvar': 'string' }) self.assertEqual(scope.tosend, {'routine': 1, 'routine2': 'ok'}) scope = Scope() phrase = Phrase("Hello world", scope) phrase.accept() self.assertEqual(scope.toset, {}) self.assertEqual(scope.tosend, {})
def repl_core(input_file, output_file): base_scope = create_built_in_scope() scope = Scope(base_scope) parser = Parser(file_token_stream(input_file)) for paragraph in parser.iter_paragraph(scope): result = evaluate(paragraph, scope) if isinstance(result, Action): result.do(scope) else: print(result, file=output_file)
def create_new_scope(name, global_scope): """ Helper function to create new scope :param name: :param global_scope: :return: """ curr_scope = Scope(name, global_scope) return curr_scope
def run_file(filename, check=False): """ Executes the N file at the given file path. Returns a human-readable string if there was an error or None if everything went well. """ file = None try: with open(filename, "r", encoding="utf-8") as f: file = File(f) except: print((Fore.RED + "Error" + Fore.RESET + ": Unable to read file " + Fore.YELLOW + "%s" + Fore.RESET) % filename) exit() file_path = path.abspath(filename) global_scope = Scope(base_path=path.dirname(file_path), file_path=file_path) add_funcs(global_scope) try: tree = file.parse(n_parser) except lark.exceptions.UnexpectedCharacters as e: return format_error(e, file) except lark.exceptions.UnexpectedEOF as e: return format_error(e, file) try: errors, error_count, warning_count = type_check( global_scope, file, tree, check) except Exception as err: debug = os.environ.get("N_ST_DEBUG") == "dev" if debug: raise err return stack_trace.display(global_scope.stack_trace, False) if error_count > 0 or check: error_s = "" warning_s = "" if error_count != 1: error_s = "s" if warning_count != 1: warning_s = "s" return f"{errors}\n{Fore.BLUE}Ran with {Fore.RED}{error_count} error{error_s}{Fore.BLUE} and {Fore.YELLOW}{warning_count} warning{warning_s}{Fore.BLUE}.{Style.RESET_ALL}" try: asyncio.get_event_loop().run_until_complete( parse_tree(global_scope, tree, file)) return global_scope except Exception as err: debug = os.environ.get("N_ST_DEBUG") == "dev" if debug: raise err return stack_trace.display(global_scope.stack_trace)
def test_iter_paragraph(self): scope = Scope() with patch('parse.Parser.parse_paragraph', side_effect=[1, 2, 3], autospec=True) as paragraph_mock: with patch('parse.TokenStream.not_empty', autospec=True, side_effect=[True, True, False]) as not_empty_mock: parser = fake_parser(['One', 'two']) for paragraph in parser.iter_paragraph(scope): pass self.assertEqual(2, paragraph_mock.call_count) self.assertEqual(3, not_empty_mock.call_count)
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_link_flexible_setters(self): # we can test linking at the initalization phrase = 'my name is not setted' phrase = Phrase(phrase, Scope()) self.assertEqual(phrase.flexibles, []) phrase = 'my name is `name~Mark` and i do like `objects~cats`' phrase = Phrase(phrase, Scope()) self.assertEqual(phrase.flexibles, \ [['name', 'Mark', 11, 15, 4], ['objects', 'cats', 30, 34, 9]]) # flexible setter is linked with #4 and #9 in sentence # my name is Mark and i do like cats # known bug phrase = '`object1~word` is `object2~word`' phrase = Phrase(phrase, Scope()) self.assertEqual(phrase.flexibles, \ [['object1', 'word', 0, 4, 1], ['object2', 'word', 8, 12, 3]])
def form_scope_inner(self, node: AstNode, parent: Scope, s_type: str): scope = parent if isinstance(node, VarsDeclNode): scope = self.vars_decl_scope(scope, str(node.vars_type), s_type, node.vars_list) elif isinstance(node, FuncNode): if scope.is_func_allowed: if self.check_in_scope(scope, str(node.inner.name), 'funcs'): s_type = 'local' return_type = node.type.name node.scope = scope node = node.inner scope.funcs[str(node.name)] = {'r': return_type, 'p': [str(x.children[0]) for x in node.params.vars_list]} scope = Scope(parent=scope, name=str(node.name)) for param in node.params.vars_list: param.scope = self.vars_decl_scope(scope, str(param.vars_type), 'param', param.vars_list) node.scope = scope node = node.stmts else: raise AnalyzerError("{0} has already been initialized".format(str(node.inner.name))) else: raise AnalyzerError("Impossible to initialize functions outside global scope") elif isinstance(node, (ForNode, WhileNode, DoWhileNode, IfNode)): class_name = node.__class__.__name__ scope = Scope(parent=scope, name=class_name[: len(class_name) - 4]) s_type = 'local' if len(node.children) > 0: for child in node.children: self.form_scope_inner(child, scope, s_type) else: if scope not in self.scopes: self.scopes.append(scope) self.scopes = [x for x in self.scopes if x != scope.parent] node.scope = scope
def resolveClass(node: Node, parentScope: Scope): for child in node.getChildNodes(): if child.nodeType == NodeType.CLASS: name = child.getChild('name') type_ = Type(name, True) symbol = Symbol(name, type_, SymbolKind.CLASS) child.symbol = symbol child.resolvedType = type_ parentScope.define(symbol) scope = Scope(parentScope) scope.classType = type_ child.scope = scope
def test_init(self): phrase_text = 'Hello `my_var` `my_var:32` `name~John` `routine1<"stop"`' phrase = Phrase(phrase_text, Scope()) self.assertIsInstance(phrase.scope, Scope) self.assertEqual(phrase.origin, phrase_text) self.assertEqual(phrase.phrase, "Hello `my_var` John ") self.assertEqual(phrase.substitute, [['my_var', 6, 14]]) self.assertEqual(phrase.setters, [['my_var', '32', 15, 26]]) self.assertEqual(phrase.flexibles, [['name', 'John', 16, 20, None]]) self.assertEqual(phrase.requests, [['routine1', '"stop"', 21, 38]]) self.assertEqual(phrase.latest, "Hello value John ") self.assertEqual(phrase.parsed, {'links': [], 'words': []})
def __init__(self, args, scope=Scope()): self.callable = False self.all_args = [] self.scope = scope parser = Parser({ "string": self.find_str, "integer": self.find_int, "variable": self.find_var, "argument": self.find_arg, }) parser.parse(args)