Skip to content

Commit

Permalink
Adds "./" self-reference syntax
Browse files Browse the repository at this point in the history
I'm not really committed to this bu~t, whatever.
  • Loading branch information
Altoids1 committed Mar 19, 2024
1 parent 8c65bf7 commit 841ec91
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 15 deletions.
42 changes: 37 additions & 5 deletions AST.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ Value AssignmentStatement::resolve(Interpreter& interp)

Value& lhs_val = id->handle(interp);

if(interp.error)
return Value();

if (lhs_val.t_vType == Value::vType::Function)
{
#if !defined(JOAO_SAFE) && defined(DEBUG)
Expand All @@ -91,6 +94,13 @@ Value AssignmentStatement::resolve(Interpreter& interp)
return Value();
#endif
}
if (id->class_name() == "ParentGet") {
#if !defined(JOAO_SAFE) && defined(DEBUG)
std::cerr << "WARNING: Attempted to override the 'this' pointer thing!\n";
#endif
interp.RuntimeError(this, ErrorCode::BadMemberAccess, "Cannot assign a new parent object!");
return Value();
}

switch (t_op) // FIXME: Make this suck less
{
Expand Down Expand Up @@ -183,11 +193,15 @@ Value UnaryExpression::resolve(Interpreter& interp)
case(UN_ENUMS(uOps::BitwiseNot, Value::vType::Double)): // Does, indeed, flip the bits of the double
{
double beta = rhs.t_value.as_double;
#ifdef __cpp_lib_bitops
beta = std::bit_cast<double,uint64_t>(~std::bit_cast<uint64_t,double>(beta));
#else
unsigned char* charlie = reinterpret_cast<unsigned char*>(&beta);
for(int i = 0; i < sizeof(double);++i)
{
charlie[i] = ~charlie[i];
}
#endif
return Value(beta);
}
//LENGTH
Expand Down Expand Up @@ -230,14 +244,12 @@ Value BinaryExpression::resolve(Interpreter& interp)
//The Chef's ingredients: t_op, t_lhs, t_rhs
Value lhs = t_lhs->resolve(interp);
if (interp.error) // If we somehow gained a novel error in the course of resolving the operands
{
return Value(); // Propagate that. I'm a BinaryExpression, not a TryCatch.
}

Value rhs = t_rhs->resolve(interp); //TODO: This fails the principle of short-circuiting but we'll be fine for now
if (interp.error) // Ditto.
{
return Value();
}

return BinaryOperation(lhs, rhs, t_op).get_or_throw(interp);
}

Expand Down Expand Up @@ -718,7 +730,7 @@ Value& ParentAccess::handle(Interpreter& interp)
Object* o = interp.get_objectscope();
if (!o)
{
interp.RuntimeError(this, ErrorCode::BadMemberAccess, "Cannot do ParentAccess in classless function!");
interp.RuntimeError(this, ErrorCode::BadMemberAccess, "Cannot access member in classless function!");
return Value::dev_null;
}
Function* funk = o->has_method(interp,prop.to_string());
Expand All @@ -727,6 +739,26 @@ Value& ParentAccess::handle(Interpreter& interp)
return *(o->has_property(interp,prop));
}

Value ParentGet::resolve(Interpreter& interp) {
Object* obj = interp.get_objectscope();
if (!obj) {
interp.RuntimeError(this, ErrorCode::BadMemberAccess, "Cannot acquire parent object in a classless function!");
return Value();
}
return Value(obj);
}

Value& ParentGet::handle(Interpreter& interp) {
Object* obj = interp.get_objectscope();
if (!obj) {
interp.RuntimeError(this, ErrorCode::BadMemberAccess, "Cannot acquire parent object in a classless function!");
return Value::dev_null;
}
//FIXME: This is so stupid omfg
interp.tempvalue = Value(obj);
return interp.tempvalue;
}

Value GrandparentAccess::resolve(Interpreter& interp)
{
return interp.grand_property(depth, prop, this);
Expand Down
16 changes: 16 additions & 0 deletions AST.h
Original file line number Diff line number Diff line change
Expand Up @@ -943,6 +943,22 @@ class ParentAccess : public ASTNode
virtual Value& handle(Interpreter&) override;
};

class ParentGet : public ASTNode
{
public:
ParentGet(int linenum = 0)
{
my_line = linenum;
}
virtual const std::string class_name() const override { return "ParentGet"; }
virtual std::string dump(int indent) override
{
return std::string(indent, ' ') + "ParentGet\n";
}
virtual Value resolve(Interpreter&) override;
virtual Value& handle(Interpreter&) override;
};

class GrandparentAccess : public ASTNode
{
unsigned int depth;
Expand Down
7 changes: 4 additions & 3 deletions Forward.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@
#include <cmath>
#include <cstdint>
#endif
#ifdef __cpp_lib_bitops
#include <bit>
#endif

#if __cplusplus > 202000L
#include <version>
#ifdef __cpp_lib_bitops
#include <bit>
#endif
#define LIKELY [[likely]]
#define UNLIKELY [[unlikely]]
#else
Expand Down
13 changes: 9 additions & 4 deletions Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -362,10 +362,15 @@ class Parser
break;
case(Token::cEnum::ParentToken):
{
if (there == here || tokens[here + 1]->class_enum() != Token::cEnum::WordToken)
ParserError(t, "ParentToken found with no corresponding Name!");
scoped_access = new ParentAccess(static_cast<WordToken*>(tokens[here + 1])->word);
tokenheader = here + 2;
if (there == here || tokens[here + 1]->class_enum() != Token::cEnum::WordToken) {
//ParserError(t, "ParentToken found with no corresponding Name!");
scoped_access = new ParentGet();
tokenheader = here + 1;
}
else {
scoped_access = new ParentAccess(static_cast<WordToken*>(tokens[here + 1])->word);
tokenheader = here + 2;
}
break;
}
case(Token::cEnum::GrandparentToken):
Expand Down
3 changes: 3 additions & 0 deletions Scanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,9 @@ int Scanner::readSymbol(int it, std::istream& ifst)
ascii_lower:
ascii_UPPER:
ascii_other:
case '[':
case ' ':
case '(':
append(new ParentToken(linenum, syntactic_linenum));
return it + 1;
default:
Expand Down
15 changes: 12 additions & 3 deletions testprograms/tests/mustpass/indirect_access.jao
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
}
/assert(stmt)
{
if(!stmt)
{
throw /error/stringlib/New("Indirect access is busted!");
if(!stmt) {
throw /error/access/New("Indirect access is busted!");
}
}
## Why don't we make sure that the README works?
Expand Down Expand Up @@ -46,6 +45,11 @@
return ../grandparent_method();
}

## Quick sidestep here to test table's syntactical weirdness
/table/vector/mag() {
return sqrt(./[0]^2 + ./[1]^2);
}

/main()
{
/color = "blue";
Expand All @@ -56,4 +60,9 @@
assert(pomme.get_base_color() == "red");
assert(pomme.get_glob_color() == "blue");
assert(pomme.child_method() == 6);

pomme = /table/vector/New();
pomme[0] = 4;
pomme[1] = 3;
assert(pomme.mag() == 5);
}

0 comments on commit 841ec91

Please sign in to comment.