Skip to content

Commit

Permalink
Made dictionaries more internal.
Browse files Browse the repository at this point in the history
  • Loading branch information
DevReaper0 committed Feb 21, 2022
1 parent 8d66ebd commit f3f7ff5
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 59 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 6 additions & 13 deletions examples/python_methods.py
Original file line number Diff line number Diff line change
@@ -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())
Expand All @@ -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.
20 changes: 14 additions & 6 deletions interpreter/env/builtins.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
76 changes: 48 additions & 28 deletions std/types/caseinsensitivedict.para
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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();
Expand Down Expand Up @@ -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);
Expand All @@ -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];
Expand All @@ -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) {
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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();

Expand Down
14 changes: 2 additions & 12 deletions std/types/dict.para
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down

0 comments on commit f3f7ff5

Please sign in to comment.