From f3f7ff54e5c2f2a5af93d49254368f2cf0ce3652 Mon Sep 17 00:00:00 2001 From: DaRubyMiner360 Date: Mon, 21 Feb 2022 13:40:05 -0500 Subject: [PATCH] Made dictionaries more internal. --- CHANGELOG.md | 1 + examples/python_methods.py | 19 +++----- interpreter/env/builtins.py | 20 +++++--- std/types/caseinsensitivedict.para | 76 +++++++++++++++++++----------- std/types/dict.para | 14 +----- 5 files changed, 71 insertions(+), 59 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 199d74a..76d95fc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed - Made the repl slightly nicer to look at +- Made dictionaries more internal. ## [2.0.1] - 2021-10-18 ### Fixed diff --git a/examples/python_methods.py b/examples/python_methods.py index cfc423e..aef90e3 100644 --- a/examples/python_methods.py +++ b/examples/python_methods.py @@ -1,14 +1,5 @@ from interpreter.basic_value import BasicValue -# pyimport("examples/python_methods.py", "__testq__", "dictionaries"); -# pyimport("examples/python_methods.py", "__testw__", "lists"); - -def dictionaries(arguments): - return BasicValue({"A": "B"}) - -def lists(arguments): - return BasicValue(["A", "B"]) - def p(arguments): if len(arguments.arguments) > 0: print(arguments.arguments[0].extract_value()) @@ -17,7 +8,9 @@ def p(arguments): return BasicValue("Nothing provided.") # Now just run: -# pyimport("examples/python_methods.py", "__p__", "p"); -# pyimport("examples/python_methods.py", ["__p__"], ["p"]); -# pyimport("examples/python_methods.py", Dict.new(["__p__"], ["p"])); -# To add it to ParaCode +# pyimport("examples/python_methods.py", ["p": "__p__"]); +# To add it to ParaCode. +# The first parameter is the python file's path +# The second parameter is a dictionary where the keys +# are the python function names and the values are the +# ParaCode function names. diff --git a/interpreter/env/builtins.py b/interpreter/env/builtins.py index a25e088..fac3d8f 100644 --- a/interpreter/env/builtins.py +++ b/interpreter/env/builtins.py @@ -2668,12 +2668,20 @@ def builtin_import_python(arguments): code = ("from " + str(arguments.arguments[0].extract_value()).replace(".py", "").replace("/", ".") + " import *;") else: code = str(arguments.arguments[0].extract_value()) + ";\n" - code += "interpreter._globals.variables.append(('" + str(arguments.arguments[1].extract_value()) + "', VariableType.Function, BuiltinFunction('" + str(arguments.arguments[1].extract_value()) + "', None, " - if len(arguments.arguments) > 2: - code += str(arguments.arguments[2].extract_value()) - else: - code += str(arguments.arguments[1].extract_value()) - code += "))); interpreter._globals.apply_to_scope(arguments.interpreter.current_scope);" + + if type(arguments.arguments[1].extract_value()) is dict: + for key in dict(arguments.arguments[1].extract_value()): + value = dict(arguments.arguments[1].extract_value())[key] + code += "interpreter._globals.variables.append(('" + str(value.extract_value()) + "', VariableType.Function, BuiltinFunction('" + str(value.extract_value()) + "', None, " + code += str(key.extract_value()) + code += "))); interpreter._globals.apply_to_scope(arguments.interpreter.current_scope); " + # else: + # code += "interpreter._globals.variables.append(('" + str(arguments.arguments[1].extract_value()) + "', VariableType.Function, BuiltinFunction('" + str(arguments.arguments[1].extract_value()) + "', None, " + # if len(arguments.arguments) > 2: + # code += str(arguments.arguments[2].extract_value()) + # else: + # code += str(arguments.arguments[1].extract_value()) + # code += "))); interpreter._globals.apply_to_scope(arguments.interpreter.current_scope);" exec(code) diff --git a/std/types/caseinsensitivedict.para b/std/types/caseinsensitivedict.para index 92a9e23..9124eda 100644 --- a/std/types/caseinsensitivedict.para +++ b/std/types/caseinsensitivedict.para @@ -3,14 +3,14 @@ let CaseInsensitiveDict = Dict.extend({ instance = { _value = [:] - _normal = [:] + //_normal = [:] func __at__(self, key) { return __intern_dictionary_at__(self._value, self._getcorrectkey(key)); } func __add__(self, value) { - return self.append(value[0]); + return self.append(value); } func __bitor__(self, value) { @@ -37,7 +37,7 @@ let CaseInsensitiveDict = Dict.extend({ } func __bitand__(self, value) { - let res_dictionary = [:]; + let res_dictionary = CaseInsensitiveDict.new([:]); let remaining = self.len(); let len = self.len(); @@ -73,12 +73,13 @@ let CaseInsensitiveDict = Dict.extend({ } func to_str(self) { - return self._normal.to_str(); + //return self._normal.to_str(); + return self._value.to_str(); } func from(self, index) { let len = self.len(); - let newdict = [:]; + let newdict = CaseInsensitiveDict([:]); // TODO: < not != while index < len { newdict += self.getpair(index); @@ -88,27 +89,45 @@ let CaseInsensitiveDict = Dict.extend({ } func __set__(self, key, value) { - __intern_dictionary_set__(self._value, key, value); + __intern_dictionary_set__(self._value, self._getcorrectkey(key, 3, true), value); return self; } } // Constructor func __construct__(self, value) { - self._value = [:]; - for pair in value { - self._value[self._getcorrectkey(pair[0])]; + //self._value = [:]; + //for pair in value { + // self._value[self._getcorrectkey(pair[0])] = pair[1]; + //} + //self._normal = value; + + if value.type() == CaseInsensitiveDict { + self._value = value._value; + } + else { + self._value = value; } - self._normal = value; return self; } - func _getcorrectkey(self, key) { + func _getcorrectkey(self, key, check_depth=3, keep_case=false) { + //if key.type() == Str { + // return key.tolower(); + //} + //return key; + + if check_depth == 2 || (check_depth == 3 && self.contains(key)) { + if key.type() == Str { + return self._value.getkey(self.indexatkey(key.tolower())); + } + return self._value.getkey(self.indexatkey(key)); + } if key.type() == Str { return key.tolower(); } return key; - } + } func getkey(self, index) { return self.getpair(index)[0]; @@ -119,21 +138,23 @@ let CaseInsensitiveDict = Dict.extend({ } func getpair(self, index) { - return self.atindex(index); - } - - func atindex(self, index) { return __intern_dictionary_atindex__(self._value, index); } func indexatkey(self, key) { - let key = self._getcorrectkey(key); - return __intern_dictionary_indexatkey__(self._value, key); + for i in Range.new(self.len()) { + if self._getcorrectkey(self.getkey(i), 1) == self._getcorrectkey(key, 1) { + return i; + } + } } func contains(self, key) { - let key = self._getcorrectkey(key); - return __intern_dictionary_contains__(self._value, key); + for pair in self { + if self._getcorrectkey(pair[0], 1) == self._getcorrectkey(key, 1) { + return true; + } + } } func to_array(self) { @@ -151,22 +172,21 @@ let CaseInsensitiveDict = Dict.extend({ func update(self, key, value) { let key = self._getcorrectkey(key); let result = self.clone().modify(key, value); - result._normal.modify(result._normal.getkey(result.indexatkey(key)), value); + //result._normal.modify(result._normal.getkey(result.indexatkey(key)), value); return result; } func clone(self) { - let result = __intern_dictionary_clone__(self._value); - result._normal = self._normal.clone(); + let result = CaseInsensitiveDict.new(__intern_dictionary_clone__(self._value)); + //result._normal = self._normal.clone(); return result; } func find(self, key) { - let key = self._getcorrectkey(key); let length = self.len(); let index = 0; while index != length { - if self.getkey(index) == key { + if self._getcorrectkey(self.getkey(index), 1) == self._getcorrectkey(key, 1) { return index; } index += 1; @@ -175,11 +195,11 @@ let CaseInsensitiveDict = Dict.extend({ } func findpair(self, pair) { - let pair = [pair[0], self._getcorrectkey(pair[1])]; let length = self.len(); let index = 0; while index != length { - if self.getpair(index) == pair { + let p = self.getpair(index); + if [self._getcorrectkey(p[0], 1), p[1]] == [self._getcorrectkey(pair[0], 1), pair[1]] { return index; } index += 1; @@ -188,7 +208,7 @@ let CaseInsensitiveDict = Dict.extend({ } func map(self, cb) { - let result = [:]; + let result = CaseInsensitiveDict([:]); let remaining = self.len(); let len = self.len(); diff --git a/std/types/dict.para b/std/types/dict.para index ec526e0..f9f7e49 100644 --- a/std/types/dict.para +++ b/std/types/dict.para @@ -119,24 +119,14 @@ let Dict = Iterable.extend({ } func getkey(self, index) { - let key = self.getpair(index)[0]; - - return key; + return self.getpair(index)[0]; } func getvalue(self, index) { - let value = self.getpair(index)[1]; - - return value; + return self.getpair(index)[1]; } func getpair(self, index) { - let result = self.atindex(index); - - return result; - } - - func atindex(self, index) { return __intern_dictionary_atindex__(self._value, index); }