Compare commits
No commits in common. "2d9499c0d11853098bb4156379d8c81ebb0819f0" and "3547e58f2c57a8363f5eee08af34932ea362f782" have entirely different histories.
2d9499c0d1
...
3547e58f2c
133
AST.cpp
133
AST.cpp
|
|
@ -77,12 +77,8 @@ std::shared_ptr<Value> FuncDefAST::eval(Context* parent, std::vector<std::shared
|
|||
ptr = dynamic_cast<IfAST*>(ptr)->getResult(ctx.get()).get();
|
||||
if (!ptr)
|
||||
continue;
|
||||
if (ptr->getType() == T_ReturnAST) {
|
||||
auto retAST = dynamic_cast<ReturnAST*>(ptr);
|
||||
if (this->name == retAST->getName())
|
||||
return ptr->eval(ctx.get());
|
||||
throw std::runtime_error("Return name mismatch. Closure is not implemented yet!");
|
||||
}
|
||||
if (ptr->getType() == T_ReturnAST)
|
||||
return ptr->eval(ctx.get());
|
||||
ret = ptr->eval(ctx.get());
|
||||
}
|
||||
return ret;
|
||||
|
|
@ -125,92 +121,59 @@ std::shared_ptr<ExprAST> IfAST::getResult(Context* parent) {
|
|||
}
|
||||
|
||||
|
||||
std::shared_ptr<Value> LoopForeverAST::eval(Context* parent) {
|
||||
// No context is needed
|
||||
while (true) {
|
||||
for (auto& stmt : this->body) {
|
||||
auto* ptr = stmt.get();
|
||||
if (ptr->getType() == T_IfAST)
|
||||
ptr = dynamic_cast<IfAST*>(ptr)->getResult(parent).get();
|
||||
if (!ptr)
|
||||
continue;
|
||||
if (ptr->getType() == T_ReturnAST)
|
||||
return ptr->eval(parent);
|
||||
ptr->eval(parent);
|
||||
std::shared_ptr<Value> LoopAST::eval(Context* parent) {
|
||||
// Eval start & end
|
||||
auto s = this->start->eval(parent);
|
||||
auto e = this->end->eval(parent);
|
||||
if (s->isArray() || e->isArray())
|
||||
throw std::runtime_error("Cannot eval loop start or end as integer");
|
||||
auto sc = std::dynamic_pointer_cast<SingleValue>(s);
|
||||
auto ec = std::dynamic_pointer_cast<SingleValue>(e);
|
||||
if (!sc || !ec)
|
||||
throw std::runtime_error("Unexpected error");
|
||||
|
||||
if (sc->isT()) {
|
||||
while (true) {
|
||||
// No need to create a new context
|
||||
for (auto& stmt : this->body) {
|
||||
auto* ptr = stmt.get();
|
||||
if (ptr->getType() == T_IfAST)
|
||||
ptr = dynamic_cast<IfAST*>(ptr)->getResult(parent).get();
|
||||
if (!ptr)
|
||||
continue;
|
||||
if (ptr->getType() == T_ReturnAST)
|
||||
return ptr->eval(parent);
|
||||
}
|
||||
}
|
||||
}
|
||||
throw std::runtime_error("Unexpected error");
|
||||
}
|
||||
} else {
|
||||
if (!sc->isInt() || !ec->isInt())
|
||||
throw std::runtime_error("Cannot eval loop start or end as integer");
|
||||
|
||||
std::shared_ptr <Value> LoopForAST::eval(Context* parent) {
|
||||
// Create a new context
|
||||
auto ctx = std::make_shared<Context>(parent);
|
||||
auto ss = sc->getInt();
|
||||
auto ee = ec->getInt();
|
||||
|
||||
// Eval condition
|
||||
auto s = std::dynamic_pointer_cast<SingleValue>(this->start->eval(parent)->copy());
|
||||
auto e = std::dynamic_pointer_cast<SingleValue>(this->end->eval(parent));
|
||||
// Create a new context
|
||||
auto ctx = std::make_shared<Context>(parent);
|
||||
|
||||
// Assert that s and e are numeric
|
||||
if (!s || !e || (!s->isInt() && !s->isFloat()) || (!e->isInt() && !e->isFloat()))
|
||||
throw std::runtime_error("LoopForAST: start and end must be numeric");
|
||||
|
||||
// Main loop
|
||||
while (*s <= *e) {
|
||||
// Set the variable
|
||||
ctx->setVariable(this->name, s);
|
||||
|
||||
// Eval body
|
||||
for (auto& stmt : this->body) {
|
||||
auto* ptr = stmt.get();
|
||||
if (ptr->getType() == T_IfAST)
|
||||
ptr = dynamic_cast<IfAST*>(ptr)->getResult(ctx.get()).get();
|
||||
if (!ptr)
|
||||
continue;
|
||||
if (ptr->getType() == T_ReturnAST)
|
||||
return ptr->eval(ctx.get());
|
||||
ptr->eval(ctx.get());
|
||||
}
|
||||
|
||||
// Increment
|
||||
s->operator++();
|
||||
}
|
||||
|
||||
// Return nil
|
||||
return std::make_shared<SingleValue>(false);
|
||||
}
|
||||
|
||||
std::shared_ptr <Value> LoopDoTimesAST::eval(Context* parent) {
|
||||
// Create a new context
|
||||
auto ctx = std::make_shared<Context>(parent);
|
||||
|
||||
// Eval condition
|
||||
auto terminate = std::dynamic_pointer_cast<SingleValue>(this->times->eval(parent)->copy());
|
||||
if (!terminate || !terminate->isInt())
|
||||
throw std::runtime_error("DOTIMES: times must be an integer");
|
||||
auto n = terminate->getInt();
|
||||
|
||||
// Main Loop
|
||||
for (std::int64_t i = 0; i < n; ++i) {
|
||||
// Set Variable
|
||||
ctx->setVariable(this->name, std::make_shared<SingleValue>(i));
|
||||
|
||||
// Eval Body
|
||||
for (auto& stmt : this->body) {
|
||||
auto* ptr = stmt.get();
|
||||
if (ptr->getType() == T_IfAST)
|
||||
ptr = dynamic_cast<IfAST*>(ptr)->getResult(ctx.get()).get();
|
||||
if (!ptr)
|
||||
continue;
|
||||
if (ptr->getType() == T_ReturnAST)
|
||||
return ptr->eval(ctx.get());
|
||||
ptr->eval(ctx.get());
|
||||
for (auto i = ss; i <= ee; ++i) {
|
||||
ctx->setVariable(this->loopVar, std::make_shared<SingleValue>(i));
|
||||
for (auto& stmt : this->body) {
|
||||
auto* ptr = stmt.get();
|
||||
if (ptr->getType() == T_IfAST)
|
||||
ptr = dynamic_cast<IfAST*>(ptr)->getResult(ctx.get()).get();
|
||||
if (!ptr)
|
||||
continue;
|
||||
if (ptr->getType() == T_ReturnAST)
|
||||
return ptr->eval(ctx.get());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Return nil
|
||||
return std::make_shared<SingleValue>(false);
|
||||
return std::make_shared<SingleValue>();
|
||||
}
|
||||
|
||||
|
||||
std::shared_ptr<Value> UnaryAST::eval(Context* parent) {
|
||||
auto val = this->expr->eval(parent);
|
||||
auto valS = std::dynamic_pointer_cast<SingleValue>(val);
|
||||
|
|
@ -371,7 +334,7 @@ std::shared_ptr<Value> ListAST::eval(Context* parent) {
|
|||
std::transform(vals2.begin(), vals2.end(), std::back_inserter(intVal), [&](std::shared_ptr<SingleValue>& ptr) {
|
||||
return ptr->getInt();
|
||||
});
|
||||
return std::make_shared<SingleValue>(std::accumulate(intVal.begin() + 1, intVal.end(), intVal[0], [this](std::int64_t x, std::int64_t y) {
|
||||
return std::make_shared<SingleValue>(std::reduce(intVal.begin() + 1, intVal.end(), intVal[0], [this](std::int64_t x, std::int64_t y) {
|
||||
switch (this->op) {
|
||||
case LOGAND:
|
||||
return x & y;
|
||||
|
|
@ -423,12 +386,12 @@ std::shared_ptr<Value> ListAST::eval(Context* parent) {
|
|||
std::transform(vals2.begin(), vals2.end(), std::back_inserter(floatVal), [&](std::shared_ptr<SingleValue>& ptr) {
|
||||
return (ptr->isFloat() ? ptr->getFloat() : ptr->getInt());
|
||||
});
|
||||
return std::make_shared<SingleValue>(std::accumulate(floatVal.begin() + 1, floatVal.end(), floatVal[0], opFunc));
|
||||
return std::make_shared<SingleValue>(std::reduce(floatVal.begin() + 1, floatVal.end(), floatVal[0], opFunc));
|
||||
} else {
|
||||
std::transform(vals2.begin(), vals2.end(), std::back_inserter(intVal), [&](std::shared_ptr<SingleValue>& ptr) {
|
||||
return ptr->getInt();
|
||||
});
|
||||
return std::make_shared<SingleValue>(std::accumulate(intVal.begin() + 1, intVal.end(), intVal[0], opFunc));
|
||||
return std::make_shared<SingleValue>(std::reduce(intVal.begin() + 1, intVal.end(), intVal[0], opFunc));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
52
AST.h
52
AST.h
|
|
@ -17,9 +17,6 @@ enum ASTType {
|
|||
T_FuncCallAST,
|
||||
T_IfAST,
|
||||
T_LoopAST,
|
||||
T_LoopForeverAST,
|
||||
T_LoopForAST,
|
||||
T_LoopDoTimesAST,
|
||||
T_UnaryAST,
|
||||
T_BinaryAST,
|
||||
T_ListAST,
|
||||
|
|
@ -135,52 +132,20 @@ public:
|
|||
}
|
||||
};
|
||||
|
||||
class LoopAST : public ExprAST {};
|
||||
|
||||
class LoopForeverAST : public LoopAST {
|
||||
class LoopAST : public ExprAST {
|
||||
private:
|
||||
std::vector<std::shared_ptr<ExprAST>> body;
|
||||
|
||||
public:
|
||||
explicit LoopForeverAST(std::vector<std::shared_ptr<ExprAST>> body) : body(std::move(body)) {}
|
||||
|
||||
std::shared_ptr<Value> eval(Context* parent) override final;
|
||||
|
||||
inline ASTType getType() const override final {
|
||||
return T_LoopForeverAST;
|
||||
}
|
||||
};
|
||||
|
||||
class LoopForAST : public LoopAST {
|
||||
private:
|
||||
std::string name;
|
||||
std::string loopVar;
|
||||
std::shared_ptr<ExprAST> start;
|
||||
std::shared_ptr<ExprAST> end;
|
||||
std::vector<std::shared_ptr<ExprAST>> body;
|
||||
|
||||
public:
|
||||
LoopForAST(std::string name, std::shared_ptr<ExprAST> start, std::shared_ptr<ExprAST> end, std::vector<std::shared_ptr<ExprAST>> body) : name(std::move(name)), start(std::move(start)), end(std::move(end)), body(std::move(body)) {}
|
||||
LoopAST(std::string loopVar, std::shared_ptr<ExprAST> start, std::shared_ptr<ExprAST> end, std::vector<std::shared_ptr<ExprAST>> body) : loopVar(std::move(loopVar)), start(std::move(start)), end(std::move(end)), body(std::move(body)) {}
|
||||
|
||||
std::shared_ptr<Value> eval(Context* parent) override final;
|
||||
|
||||
inline ASTType getType() const override final {
|
||||
return T_LoopForAST;
|
||||
}
|
||||
};
|
||||
|
||||
class LoopDoTimesAST : public LoopAST {
|
||||
private:
|
||||
std::string name;
|
||||
std::shared_ptr<ExprAST> times;
|
||||
std::vector<std::shared_ptr<ExprAST>> body;
|
||||
|
||||
public:
|
||||
LoopDoTimesAST(std::string name, std::shared_ptr<ExprAST> times, std::vector<std::shared_ptr<ExprAST>> body) : name(std::move(name)), times(std::move(times)), body(std::move(body)) {}
|
||||
|
||||
std::shared_ptr<Value> eval(Context* parent) override final;
|
||||
|
||||
inline ASTType getType() const override final {
|
||||
return T_LoopDoTimesAST;
|
||||
return T_LoopAST;
|
||||
}
|
||||
};
|
||||
|
||||
|
|
@ -265,19 +230,12 @@ public:
|
|||
class ReturnAST : public ExprAST {
|
||||
private:
|
||||
std::shared_ptr<ExprAST> expr;
|
||||
std::string name;
|
||||
|
||||
public:
|
||||
explicit ReturnAST(std::shared_ptr<ExprAST> expr) : expr(std::move(expr)), name() {}
|
||||
|
||||
ReturnAST(std::shared_ptr<ExprAST> expr, std::string name) : expr(std::move(expr)), name(std::move(name)) {}
|
||||
explicit ReturnAST(std::shared_ptr<ExprAST> expr) : expr(std::move(expr)) {}
|
||||
|
||||
std::shared_ptr<Value> eval(Context* parent) override final;
|
||||
|
||||
std::string getName() const {
|
||||
return name;
|
||||
}
|
||||
|
||||
inline ASTType getType() const override final {
|
||||
return T_ReturnAST;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -60,7 +60,6 @@ aref [aA][rR][eE][fF]
|
|||
t [tT]
|
||||
nil [nN][iI][lL]
|
||||
return [rR][eE][tT][uU][rR][nN]
|
||||
returnfrom [rR][eE][tT][uU][rR][nN][-][fF][rR][oO][mM]
|
||||
makearray [mM][aA][kK][eE][-][aA][rR][rR][aA][yY]
|
||||
defconstant [dD][eE][fF][cC][oO][nN][sS][tT][aA][nN][tT]
|
||||
|
||||
|
|
@ -334,11 +333,6 @@ defconstant [dD][eE][fF][cC][oO][nN][sS][tT][aA][nN][tT]
|
|||
return token::TOKEN_RETURN;
|
||||
};
|
||||
|
||||
{returnfrom} {
|
||||
std::printf("Scanned returnfrom\n");
|
||||
return token::TOKEN_RETURN_FROM;
|
||||
};
|
||||
|
||||
{makearray} {
|
||||
std::printf("Scanned makearray\n");
|
||||
return token::TOKEN_MAKE_ARRAY;
|
||||
|
|
|
|||
|
|
@ -88,7 +88,6 @@ namespace DragonLisp {
|
|||
T "t"
|
||||
NIL "nil"
|
||||
RETURN "return"
|
||||
RETURN_FROM "return-from"
|
||||
MAKE_ARRAY "make-array"
|
||||
DEFCONSTANT "defconstant"
|
||||
;
|
||||
|
|
@ -160,7 +159,6 @@ array-ref
|
|||
|
||||
return-expr
|
||||
: LPAREN RETURN R-Value RPAREN { std::printf("Parsed return-expr -> ( RETURN R-Value )\n"); $$ = drv.constructReturnAST($3); }
|
||||
| LPAREN RETURN_FROM IDENTIFIER R-Value RPAREN { std::printf("Parsed return-expr -> ( RETURN_FROM IDENTIFIER R-Value )\n"); $$ = drv.constructReturnAST($4, $3); }
|
||||
;
|
||||
|
||||
func-body-expr
|
||||
|
|
|
|||
|
|
@ -98,26 +98,26 @@ std::shared_ptr<ReturnAST> DLDriver::constructReturnAST(std::shared_ptr<ExprAST>
|
|||
return std::make_shared<ReturnAST>(std::move(value));
|
||||
}
|
||||
|
||||
std::shared_ptr <ReturnAST> DLDriver::constructReturnAST(std::shared_ptr <ExprAST> value, std::string name) {
|
||||
return std::make_shared<ReturnAST>(std::move(value), std::move(name));
|
||||
}
|
||||
|
||||
std::shared_ptr<LoopAST> DLDriver::constructLoopAST(std::vector<std::shared_ptr<ExprAST>> body) {
|
||||
return std::make_shared<LoopForeverAST>(
|
||||
return std::make_shared<LoopAST>(
|
||||
"",
|
||||
std::make_shared<LiteralAST>(true),
|
||||
std::make_shared<LiteralAST>(true),
|
||||
std::move(body)
|
||||
);
|
||||
}
|
||||
|
||||
std::shared_ptr<LoopAST> DLDriver::constructLoopAST(std::string id, std::shared_ptr<ExprAST> to, std::vector<std::shared_ptr<ExprAST>> body) {
|
||||
return std::make_shared<LoopDoTimesAST>(
|
||||
return std::make_shared<LoopAST>(
|
||||
std::move(id),
|
||||
std::make_shared<LiteralAST>(std::int64_t(0)),
|
||||
std::move(to),
|
||||
std::move(body)
|
||||
);
|
||||
}
|
||||
|
||||
std::shared_ptr<LoopAST> DLDriver::constructLoopAST(std::string id, std::shared_ptr<ExprAST> from, std::shared_ptr<ExprAST> to, std::vector<std::shared_ptr<ExprAST>> body) {
|
||||
return std::make_shared<LoopForAST>(
|
||||
return std::make_shared<LoopAST>(
|
||||
std::move(id),
|
||||
std::move(from),
|
||||
std::move(to),
|
||||
|
|
|
|||
|
|
@ -68,7 +68,6 @@ public:
|
|||
|
||||
// Return AST
|
||||
static std::shared_ptr<ReturnAST> constructReturnAST(std::shared_ptr<ExprAST> value);
|
||||
static std::shared_ptr<ReturnAST> constructReturnAST(std::shared_ptr<ExprAST> value, std::string name);
|
||||
|
||||
// Loop AST
|
||||
static std::shared_ptr<LoopAST> constructLoopAST(std::vector<std::shared_ptr<ExprAST>> body);
|
||||
|
|
|
|||
24
sample.lisp
24
sample.lisp
|
|
@ -32,11 +32,7 @@
|
|||
)
|
||||
)
|
||||
|
||||
; Time complexity very very very high!!!
|
||||
; This is a recursive function.
|
||||
; O(2^n) time complexity.
|
||||
; Since this interpreter is not well optimized, it will take a long time to run this function.
|
||||
(print (fibonacci 8)) ; returns 34
|
||||
(fibonacci 15) ; returns 987
|
||||
|
||||
; This is a comment.
|
||||
; This is another comment.
|
||||
|
|
@ -52,7 +48,7 @@
|
|||
) ; prints 1 2 3 4 5 6 7 8 9 10
|
||||
|
||||
(defvar i 1)
|
||||
(print (loop (if (> i 5) (return i)) (incf i 3))) ; returns 7
|
||||
(loop (if (> i 5) (return i)) (incf i 3)) ; returns 7
|
||||
|
||||
(dotimes (i 10) (print i)) ; prints 0 1 2 3 4 5 6 7 8 9
|
||||
|
||||
|
|
@ -62,19 +58,3 @@
|
|||
|
||||
(DoTiMeS (i (+ 99999 -99989)) (print i) (setf (aref arr i) (* i i i))) ; prints 0 1 2 3 4 5 6 7 8 9
|
||||
(dOtImEs (i 10) (print (aref arr i))) ; prints 0 1 8 27 64 125 216 343 512 729
|
||||
|
||||
(defvar dp (make-array 90))
|
||||
(loop for i from 0 to 89 do (setf (aref dp i) -1))
|
||||
|
||||
(defun fibFast (n)
|
||||
(if (>= n 90) (return-from fibFast "I can't handle this number!"))
|
||||
(if (>= (aref dp n) 0) (return-from fibFast (aref dp n)))
|
||||
(if (<= n 1) (return-from fibFast 1))
|
||||
(setf (aref dp n) (+ (fibFast (- n 1)) (fibFast (- n 2))))
|
||||
)
|
||||
|
||||
; This is a dynamic programming function,
|
||||
; still recursive, but with a memoization table.
|
||||
; O(n) time complexity.
|
||||
(print (fibFast 8)) ; prints 34
|
||||
(print (fibFast 80)) ; prints 37889062373143906
|
||||
|
|
|
|||
2
token.h
2
token.h
|
|
@ -50,8 +50,6 @@ enum Token {
|
|||
DOLIST,
|
||||
DO,
|
||||
AREF,
|
||||
RETURN,
|
||||
RETURN_FROM,
|
||||
MAKE_ARRAY,
|
||||
DEFCONSTANT,
|
||||
};
|
||||
|
|
|
|||
56
value.h
56
value.h
|
|
@ -106,62 +106,6 @@ public:
|
|||
bool operator==(const SingleValue& rhs) const {
|
||||
return type == rhs.type && value == rhs.value;
|
||||
}
|
||||
|
||||
bool operator<(const SingleValue& rhs) const {
|
||||
if ((!this->isInt() && !this->isFloat()) || (!rhs.isInt() && !rhs.isFloat()))
|
||||
throw std::runtime_error("Cannot compare non-numeric values");
|
||||
if (this->isInt() && rhs.isInt())
|
||||
return this->getInt() < rhs.getInt();
|
||||
if (this->isInt())
|
||||
return this->getInt() < rhs.getFloat();
|
||||
if (rhs.isInt())
|
||||
return this->getFloat() < rhs.getInt();
|
||||
return this->getFloat() < rhs.getFloat();
|
||||
}
|
||||
|
||||
bool operator<=(const SingleValue& rhs) const {
|
||||
if ((!this->isInt() && !this->isFloat()) || (!rhs.isInt() && !rhs.isFloat()))
|
||||
throw std::runtime_error("Cannot compare non-numeric values");
|
||||
if (this->isInt() && rhs.isInt())
|
||||
return this->getInt() <= rhs.getInt();
|
||||
if (this->isInt())
|
||||
return this->getInt() <= rhs.getFloat();
|
||||
if (rhs.isInt())
|
||||
return this->getFloat() <= rhs.getInt();
|
||||
return this->getFloat() <= rhs.getFloat();
|
||||
}
|
||||
|
||||
SingleValue& operator++() {
|
||||
if (this->isInt())
|
||||
this->value = this->getInt() + 1;
|
||||
else if (this->isFloat())
|
||||
this->value = this->getFloat() + 1;
|
||||
else
|
||||
throw std::runtime_error("Cannot increment non-numeric value");
|
||||
return *this;
|
||||
}
|
||||
|
||||
SingleValue& operator--() {
|
||||
if (this->isInt())
|
||||
this->value = this->getInt() - 1;
|
||||
else if (this->isFloat())
|
||||
this->value = this->getFloat() - 1;
|
||||
else
|
||||
throw std::runtime_error("Cannot decrement non-numeric value");
|
||||
return *this;
|
||||
}
|
||||
|
||||
SingleValue operator++(int) {
|
||||
SingleValue tmp(*this);
|
||||
operator++();
|
||||
return tmp;
|
||||
}
|
||||
|
||||
SingleValue operator--(int) {
|
||||
SingleValue tmp(*this);
|
||||
operator--();
|
||||
return tmp;
|
||||
}
|
||||
};
|
||||
|
||||
class ArrayValue : public Value {
|
||||
|
|
|
|||
Loading…
Reference in New Issue