Skip to content

Commit

Permalink
Fixed dictionaries.
Browse files Browse the repository at this point in the history
  • Loading branch information
DevReaper0 committed Feb 23, 2022
1 parent b4511b8 commit 59c0364
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 79 deletions.
22 changes: 22 additions & 0 deletions doc/00_vars.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,25 @@ let mynum: int = 5;
Creates a variable named mynum,
with a type of `int`, and assigns
its value to 5.

Then, if you want to allow the
variable to attempt to auto-cast
to the given type, you can add
a `?`

Here's an example from the
enum documentation:

```typescript
let b: ExampleEnum = ExampleEnum.HELLO;
```

wouldn't work because `ExampleEnum.HELLO`
is actually an integer, but

```typescript
let b: ExampleEnum? = ExampleEnum.HELLO;
```

*would* work because it is allowed to
use the constructor.
70 changes: 29 additions & 41 deletions interpreter/env/builtins.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,7 @@ def builtin_array_len(arguments):
return BasicValue(len(arguments.arguments[0].extract_value()))

def builtin_dictionary_len(arguments):
if type(arguments.arguments[0].extract_value()) == dict:
return BasicValue(len(arguments.arguments[0].extract_value()))
return BasicValue(len(arguments.arguments[0].extract_value()[0]))
return BasicValue(len(arguments.arguments[0].extract_value()))

def builtin_array_set(arguments):
array = arguments.arguments[0].extract_value()
Expand Down Expand Up @@ -203,47 +201,35 @@ def builtin_array_at(arguments):

def builtin_dictionary_at(arguments):
obj = arguments.arguments[0].extract_value()
key = arguments.arguments[1]

print(key)
print(type(key))
# print(obj)
# print(list(obj.keys()))
print(list(obj.keys())[0])
print(type(list(obj.keys())[0]))
print()
print(repr(key))
print(repr(list(obj.keys())[0]))
print()
print(list(obj.keys())[0] is key)
print(list(obj.keys())[0] == key)
print(BasicValue(list(obj.keys())[0].extract_value()) == BasicValue(key.extract_value()))
if key not in obj:
print("NOOOOOOOO")
# TODO throw internal exception
return BasicValue(None)

return BasicValue(obj[key])
key = arguments.arguments[1].extract_value()

for i in obj:
k = i
if isinstance(k, BasicValue):
k = k.extract_value()
if k == key:
return BasicValue(obj[i])

return BasicValue(None)

def builtin_dictionary_atindex(arguments):
obj = arguments.arguments[0].extract_value()
index = arguments.arguments[1].extract_value()

if index >= len(obj):
# TODO throw internal exception
return BasicValue(None)

return BasicValue([list(obj)[index], obj[list(obj)[index]]])

def builtin_dictionary_indexatkey(arguments):
obj = arguments.arguments[0].extract_value()
key = arguments.arguments[1].extract_value()

if key not in obj:
# TODO throw internal exception
return BasicValue(None)
for i in range(len(obj)):
k = list(obj.keys())[i]
if isinstance(k, BasicValue):
k = k.extract_value()
if k == key:
return BasicValue(i)

return BasicValue(list(obj.keys()).index(key))
return BasicValue(-1)

def builtin_dictionary_keys(arguments):
obj = arguments.arguments[0].extract_value()
Expand All @@ -257,15 +243,17 @@ def builtin_dictionary_values(arguments):

def builtin_dictionary_contains(arguments):
obj = arguments.arguments[0].extract_value()
key = arguments.arguments[1]

print(obj)
print(list(obj.keys()))
print(list(obj.keys())[0])
print(type(list(obj.keys())[0]))
print(key)
print(type(key))
return BasicValue(key in obj)
key = arguments.arguments[1].extract_value()

found = False
for i in obj:
k = i
if isinstance(k, BasicValue):
k = k.extract_value()
if k == key:
found = True

return BasicValue(int(found))

def builtin_array_append(arguments):
interpreter = arguments.interpreter
Expand Down
3 changes: 2 additions & 1 deletion std/def.para
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,5 @@ import "std/types/exceptions/typeerror.para";
import "std/types/exceptions/interruptederror.para";
import "std/types/exceptions/notimplementederror.para";
import "std/types/exceptions/lookuperror.para";
import "std/types/exceptions/indexerror.para";
import "std/types/exceptions/arraylookuperror.para";
import "std/types/exceptions/dictlookuperror.para";
2 changes: 1 addition & 1 deletion std/types/array.para
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ let Array = Iterable.extend({

func __at__(self, index: int) {
if index >= self.len() {
IndexError.new("Array index out of range!").raise();
ArrayLookupError.new("Index out of range!").raise();
return null;
}
let value = __intern_array_at__(self._value, index);
Expand Down
53 changes: 29 additions & 24 deletions std/types/caseinsensitivedict.para
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ let CaseInsensitiveDict = Dict.extend({

instance = {
_value = [:]
//_normal = [:]

func __at__(self, key) {
if !self.contains(key) {
DictLookupError.new("Key doesn't exist!").raise();
return null;
}
return __intern_dictionary_at__(self._value, self._getcorrectkey(key));
}

Expand Down Expand Up @@ -73,14 +76,12 @@ let CaseInsensitiveDict = Dict.extend({
}

func to_str(self) {
//return self._normal.to_str();
return self._value.to_str();
}

func from(self, index) {
let len = self.len();
let newdict = CaseInsensitiveDict([:]);
// TODO: < not !=
while index < len {
newdict += self.getpair(index);
index += 1;
Expand All @@ -95,35 +96,31 @@ let CaseInsensitiveDict = Dict.extend({
}

// Constructor
func __construct__(self, value) {
//self._value = [:];
//for pair in value {
// self._value[self._getcorrectkey(pair[0])] = pair[1];
//}
//self._normal = value;

func __construct__(self, value, values=null) {
if value.type() == CaseInsensitiveDict {
self._value = value._value;
}
else {
self._value = value;
if values != null {
for i in Range.new(0, value.len()) {
self[value[i]] = values[i];
}
}
else {
self._value = value;
}
}
return self;
}

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 {
if key.type() == Str && !keep_case {
return self._value.getkey(self.indexatkey(key.tolower()));
}
return self._value.getkey(self.indexatkey(key));
}
if key.type() == Str {
if key.type() == Str && !keep_case {
return key.tolower();
}
return key;
Expand All @@ -142,6 +139,11 @@ let CaseInsensitiveDict = Dict.extend({
}

func indexatkey(self, key) {
if !self.contains(key) {
DictLookupError.new("Key doesn't exist!").raise();
return null;
}

for i in Range.new(self.len()) {
if self._getcorrectkey(self.getkey(i), 1) == self._getcorrectkey(key, 1) {
return i;
Expand Down Expand Up @@ -170,15 +172,12 @@ 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);
let result = self.clone().modify(self._getcorrectkey(key), value);
return result;
}

func clone(self) {
let result = CaseInsensitiveDict.new(__intern_dictionary_clone__(self._value));
//result._normal = self._normal.clone();
return result;
}

Expand All @@ -199,8 +198,14 @@ let CaseInsensitiveDict = Dict.extend({
let index = 0;
while index != length {
let p = self.getpair(index);
if [self._getcorrectkey(p[0], 1), p[1]] == [self._getcorrectkey(pair[0], 1), pair[1]] {
return index;
if pair.len() == 2 {
if self._getcorrectkey(p[0], 1) == self._getcorrectkey(pair[0], 1) && p[1] == pair[1] {
return index;
}
}
else {
DictLookupError.new("Invalid pair!").raise();
return -1;
}
index += 1;
}
Expand Down
25 changes: 16 additions & 9 deletions std/types/dict.para
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@ let Dict = Iterable.extend({
_value = [:]

func __at__(self, key) {
print(self.contains(key));
if !self.contains(key) {
print("No.");
DictLookupError.new("Key doesn't exist!").raise();
return null;
}
//return __intern_dictionary_at__(self._value, key);
return self.getvalue(self.indexatkey(key));
return __intern_dictionary_at__(self._value, key);
}

func __add__(self, value) {
Expand Down Expand Up @@ -104,7 +102,6 @@ let Dict = Iterable.extend({
func from(self, index) {
let len = self.len();
let newdict = [:];
// TODO: < not !=
while index < len {
newdict += self.getpair(index);
index += 1;
Expand Down Expand Up @@ -144,12 +141,15 @@ let Dict = Iterable.extend({
}

func indexatkey(self, key) {
if !self.contains(key) {
DictLookupError.new("Key doesn't exist!").raise();
return null;
}
return __intern_dictionary_indexatkey__(self._value, key);
}

func contains(self, key) {
//return __intern_dictionary_contains__(self._value, key);
return self.keys().contains(key);
return __intern_dictionary_contains__(self._value, key);
}

func to_array(self) {
Expand Down Expand Up @@ -188,8 +188,15 @@ let Dict = Iterable.extend({
let length = self.len();
let index = 0;
while index != length {
if self.getpair(index) == pair {
return index;
let p = self.getpair(index);
if pair.len() == 2 {
if p[0] == pair[0] && p[1] == pair[1] {
return index;
}
}
else {
DictLookupError.new("Invalid pair!").raise();
return -1;
}
index += 1;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
let IndexError = LookupError.extend({
name = 'IndexError'
let ArrayLookupError = LookupError.extend({
name = 'ArrayLookupError'

instance = {
func raise(self) {
return raise(self, "Index Error");
return raise(self, "Array Lookup Error");
}
}

Expand Down
13 changes: 13 additions & 0 deletions std/types/exceptions/dictlookuperror.para
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
let DictLookupError = LookupError.extend({
name = 'DictLookupError'

instance = {
func raise(self) {
return raise(self, "Dictionary Lookup Error");
}
}

func __construct__(self, _message) {
self.message = _message;
}
});

0 comments on commit 59c0364

Please sign in to comment.