Skip to content

Commit

Permalink
Started working on enums.
Browse files Browse the repository at this point in the history
  • Loading branch information
DevReaper0 committed Feb 15, 2022
1 parent 6499009 commit 9947c32
Show file tree
Hide file tree
Showing 25 changed files with 247 additions and 84 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]
- Support for variables in the `catch` statement
- More errors
- More error messages
- Complete requests HTTP library
- Better interfacing with python code from ParaCode
- The ability to use keyword arguments when calling a function
- **kwargs (Keyword Arguments)
- More exceptions

## [2.1.0] - CURRENTLY UNRELEASED
### Added
- Easy use of default parameters in functions
- *args (Non-Keyword Arguments)
- Complete regex support
- Enums

## [2.0.1] - 2021-10-18
### Fixed
Expand Down
1 change: 1 addition & 0 deletions doc/85_enums.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
### Enums
1 change: 1 addition & 0 deletions error.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class ErrorType(Enum):
MultipleDefinition = auto()
ArgumentError = auto()
MacroExpansionError = auto()
InterruptedError = auto()

class Error():
def __init__(self, type, location, message, filename, name="Exception"):
Expand Down
13 changes: 13 additions & 0 deletions examples/enums.para
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
let ExampleEnum = Enum.extend({
name = 'ExampleEnum'

instance = {}

HELLO = 0
WORLD = 1
});

// interpreter.py: line 275
// let v: ExampleEnum = ExampleEnum.new(ExampleEnum.HELLO);
let v: ExampleEnum = ExampleEnum.HELLO;
print(v.to_str());
16 changes: 16 additions & 0 deletions interpreter/env/builtins.py
Original file line number Diff line number Diff line change
Expand Up @@ -1131,6 +1131,22 @@ def builtin_pyeval(arguments):

return BasicValue(result)

def builtin_object_members(arguments):
target = arguments.arguments[0].extract_value()

# members = {}
members = target.members.copy()

# for key in target.members:
# if isinstance(target.members[key], BasicObject):
# members[key] = target.members[key].clone(parent_override=target)
# else:
# members[key] = target.members[key]

members = [list(members.keys()), list(members.values())]

return fixiter(members)

def builtin_object_new(arguments):
interpreter = arguments.interpreter
this_object = arguments.this_object
Expand Down
1 change: 1 addition & 0 deletions interpreter/env/globals.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def __init__(self):
('Type', VariableType.Object, self.basic_type),
('Object', VariableType.Type, self.basic_object),
('Func', VariableType.Type, self.func_type),
('__intern_object_members__', VariableType.Function, BuiltinFunction("__intern_object_members__", None, builtin_object_members)),
('__intern_object_patch__', VariableType.Function, BuiltinFunction("__intern_object_patch__", None, builtin_object_patch)),
('__intern_math_e__', VariableType.Function, BuiltinFunction("__intern_math_e__", None, builtin_math_e)),
('__intern_math_inf__', VariableType.Function, BuiltinFunction("__intern_math_inf__", None, builtin_math_inf)),
Expand Down
14 changes: 12 additions & 2 deletions interpreter/interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ def error(self, node, type, message, cont=False, name=None, classnames=None, obj
elif type == ErrorType.ArgumentError:
name = "Argument Error"
classnames = ["Exception", "Argument Error"]
elif type == ErrorType.InterruptedError:
name = "Interrupted Error"
classnames = ["Exception", "InterruptedError"]
elif type == ErrorType.MacroExpansionError:
name = "Macro Expansion Error"
classnames = ["Exception", "MacroExpansionError"]
Expand Down Expand Up @@ -269,8 +272,15 @@ def assignment_typecheck(self, node, type_object, assignment_value):
return False

if not type_object.compare_type(assignment_type):
self.error(node, ErrorType.TypeError, 'Attempted to assign <{}> to a value of type <{}>'.format(type_object.friendly_typename, assignment_type.friendly_typename))
return False
try:
# target_type_object = assignment_type.lookup_type(self.global_scope).extract_basicvalue()
# type_object
# assignment_type
# assignment_value
builtin_object_new(BuiltinFunctionArguments(interpreter=self, this_object=assignment_type, arguments=[type_object], node=node))
except InterpreterError:
self.error(node, ErrorType.TypeError, 'Attempted to assign <{}> to a value of type <{}>'.format(type_object.friendly_typename, assignment_type.friendly_typename))
return False

return True

Expand Down
6 changes: 5 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
#!/bin/python3

import sys

# TODO: Implement version checker with `vermin --backport enum --no-parse-comments --versions .`

import os
from ParaCode import ParaCode
from parse.parser import Parser

import sys
# TODO: Move installDependencies.py and dataCounter.py's code here

def main():
os.system("")
Expand Down
2 changes: 0 additions & 2 deletions std/__repl__.para
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@ func tictactoe() {

let sep_str = '\n' + (' ' * 22) + ('-' * 30) + '\n';

io.write(sep_str);
io.write_color(Console.RED, ' *** ');
io.write_color(Console.BOLD, "Run `tictactoe();` to start a game of TicTacToe (built 100% in ParaCode!)");
io.write_color(Console.RED, ' *** ');
io.write('\n');
io.write((' ' * 8) + 'Code in examples/ttt.para. More examples in that folder as well!');
io.write(sep_str);
io.write('\n');
55 changes: 17 additions & 38 deletions std/def.para
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ __intern_object_patch__(Object, {
func __eql__(self, other) {
return __intern_default_compare__(self, other);
}

func members(self) {
let result = __intern_object_members__(self);
return Dict.new(result[0], result[1]);
}
});

Type.patch({
Expand All @@ -15,6 +20,15 @@ Type.patch({
}
});

// These are only set so other code works fine before the actual io code gets run.
let io = Type.extend({
func write(*args) {}
func read(*args) {}
});
func print(message="", end="\n") {
io.write(message, end);
}

let pyimport = __intern_import_python__;


Expand All @@ -32,48 +46,13 @@ import "std/types/caseinsensitivedict.para";
import "std/types/str.para";
import "std/types/range.para";
import "std/types/macro.para";
import "std/types/enum.para";

import "std/types/exceptions/exception.para";
import "std/types/exceptions/argumenterror.para";
import "std/types/exceptions/doesnotexisterror.para";
import "std/types/exceptions/macroexpansionerror.para";
import "std/types/exceptions/multipledefinitionerror.para";
import "std/types/exceptions/typeerror.para";

// Type aliases
let object = Object;
let float = Float;
let type = Type;

let array = Array;
let Arr = Array;
let arr = Array;

let dict = Dict;
let Dictionary = Dict;
let dictionary = Dict;

let caseinsensitivedict = CaseInsensitiveDict;
let CaseInsensitiveDictionary = CaseInsensitiveDict;
let caseinsensitivedictionary = CaseInsensitiveDict;
let InsensitiveDictionary = CaseInsensitiveDict;
let insensitivedictionary = CaseInsensitiveDict;

let num = Num;
let number = Num;
let Number = Num;

let int = Int;
let Integer = Int;
let integer = Int;

let str = Str;
let String = Str;
let string = Str;

let bool = Bool;
let Boolean = Bool;
let boolean = Bool;

let Function = Func;
let function = Func;
import "std/types/exceptions/interruptederror.para";
import "std/types/exceptions/notimplementederror.para";
4 changes: 2 additions & 2 deletions std/io.para
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
let io = Type.extend({
io = Type.extend({
func pyeval(self, *args) {
return __intern_pyeval__(*args);
}
Expand All @@ -7,4 +7,4 @@ let io = Type.extend({
import "std/io/file.para";
import "std/io/console.para";

let print = io.write;
// print = io.write;
5 changes: 5 additions & 0 deletions std/types/array.para
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,8 @@ let Array = Iterable.extend({
return self.len();
}
});

// Type aliases
let array = Array;
let Arr = Array;
let arr = Array;
7 changes: 6 additions & 1 deletion std/types/bool.para
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ let Bool = Num.extend({
func _invert(self, value) {
return value.__not__();
}

func __bool__(self) {
return self._value;
}
Expand Down Expand Up @@ -45,3 +45,8 @@ let true = Bool.true;
let True = Bool.true;
let false = Bool.false;
let False = Bool.false;

// Type aliases
let bool = Bool;
let Boolean = Bool;
let boolean = Bool;
31 changes: 15 additions & 16 deletions std/types/caseinsensitivedict.para
Original file line number Diff line number Diff line change
Expand Up @@ -200,21 +200,17 @@ let CaseInsensitiveDict = Dict.extend({
self._normal = Dict.new(self._normal[0], self._normal[1]);
}

let result = [null, null];
let result = null;

for i in Range.new(0, self.len()) {
if self._value[0][i].tolower() == aindex.tolower() {
let key = self._normal._value[0][i];
let value = self._value[1][i];
result = [key, value];
result = self._value[1][i];
}
else {
if i == self.len() - 1 && result == [null, null] {
if i == self.len() - 1 && result == null {
let intIndex = self.len() - 1;
if intIndex != -1 {
let key = self._normal._value[0][intIndex];
let value = self._value[1][intIndex];
result = [key, value];
result = self._value[1][intIndex];
}
}
}
Expand All @@ -230,21 +226,17 @@ let CaseInsensitiveDict = Dict.extend({
self._normal = Dict.new(self._normal[0], self._normal[1]);
}

let result = [null, null];
let result = null;

for i in Range.new(0, self.len()) {
if self._value[1][i] == aindex {
let key = self._normal._value[0][i];
let value = self._value[1][i];
result = [key, value];
result = self._normal._value[0][i];
}
else {
if i == self.len() - 1 && result == [null, null] {
if i == self.len() - 1 && result == null {
let intIndex = self.len() - 1;
if intIndex != -1 {
let key = self._normal._value[0][intIndex];
let value = self._value[1][intIndex];
result = [key, value];
result = self._normal._value[0][intIndex];
}
}
}
Expand Down Expand Up @@ -336,3 +328,10 @@ let CaseInsensitiveDict = Dict.extend({
return result;
}
});

// Type aliases
let caseinsensitivedict = CaseInsensitiveDict;
let CaseInsensitiveDictionary = CaseInsensitiveDict;
let caseinsensitivedictionary = CaseInsensitiveDict;
let InsensitiveDictionary = CaseInsensitiveDict;
let insensitivedictionary = CaseInsensitiveDict;
30 changes: 14 additions & 16 deletions std/types/dict.para
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ let Dict = Iterable.extend({
// Constructor
func __construct__(self, keys, values) {
self._value = [keys, values];
return self;
}

func setOrAppend(self, key, value) {
Expand Down Expand Up @@ -177,21 +178,17 @@ let Dict = Iterable.extend({
}

func atkey(self, aindex) {
let result = [null, null];
let result = null;

for i in Range.new(0, self.len()) {
if self._value[0][i] == aindex {
let key = self._value[0][i];
let value = self._value[1][i];
result = [key, value];
result = self._value[1][i];
}
else {
if i == self.len() - 1 && result == [null, null] {
if i == self.len() - 1 && result == null {
let intIndex = self.len() - 1;
if intIndex != -1 {
let key = self._value[0][intIndex];
let value = self._value[1][intIndex];
result = [key, value];
result = self._value[1][intIndex];
}
}
}
Expand All @@ -203,21 +200,17 @@ let Dict = Iterable.extend({


func atvalue(self, aindex) {
let result = [null, null];
let result = null;

for i in Range.new(0, self.len()) {
if self._value[1][i] == aindex {
let key = self._value[0][i];
let value = self._value[1][i];
result = [key, value];
result = self._value[0][i];
}
else {
if i == self.len() - 1 && result == [null, null] {
if i == self.len() - 1 && result == null {
let intIndex = self.len() - 1;
if intIndex != -1 {
let key = self._value[0][intIndex];
let value = self._value[1][intIndex];
result = [key, value];
result = self._value[0][intIndex];
}
}
}
Expand Down Expand Up @@ -315,3 +308,8 @@ let Dict = Iterable.extend({
return self._value[1];
}
});

// Type aliases
let dict = Dict;
let Dictionary = Dict;
let dictionary = Dict;
Loading

0 comments on commit 9947c32

Please sign in to comment.