Skip to content
This repository was archived by the owner on Oct 12, 2026. It is now read-only.

Commit 0593bd3

Browse files
committed
Fixing apply_fn_apl
1 parent abbed7d commit 0593bd3

9 files changed

Lines changed: 315 additions & 83 deletions

File tree

‎CMakeLists.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ add_library(fe STATIC
1818
add_library(intp STATIC
1919
${CMAKE_SOURCE_DIR}/src/intp/types.cpp
2020
${CMAKE_SOURCE_DIR}/src/intp/interpreter.cpp
21+
src/intp/buitlins.cpp
2122
)
2223

2324
add_executable(lambda_discipline ${CMAKE_SOURCE_DIR}/src/main.cpp)

‎examples/math_demos.lbd‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
-- TODO: Add support for `use`
2+
-- Copied from std.lbd
3+
-- booleans and conditions
4+
true: Bool = \x: Any. \y: Any. x
5+
false: Bool = \x: Any. \y: Any. y
6+
7+
if_then: Any = \condition: Any.
8+
\then_clause: Any. \else_clause: Any.
9+
(condition then_clause else_clause)
10+
11+
-- pairs
12+
pair: Any = \x: Any. \y: Any. \fn: Any. (fn x y)
13+
14+
first: Any = \x: Any. \y: Any. x
15+
second: Any = \x: Any. \y: Any. y
16+
17+
-- misc
18+
float_eq: Float -> Float -> Bool = \x: Float. \y: Float.
19+
(if_zero (cmp x y) true false)
20+
21+
-- fibonacci: Any = \num: Float.
22+
-- (if_then (float_eq 0.0 num) 0.0
23+
-- (if_then (float_eq 1.0 num) 1.0
24+
-- (add (fibonacci (sub num 1.0)) (fibonacci (sub num 2.0)))))
25+
26+
-- fibonacci: Any = \num: Float. num
27+
-- res: Float = \num: Float. (add (fibonacci (sub num 1)) (fibonacci (sub num 2)))
28+
-- (print (res 10))
29+
30+
fibonacci: Any = \num: Float.
31+
(if_zero (float_eq 1 num) 1
32+
(add (fibonacci (sub num 1)) (fibonacci (sub num 2))))
33+
(print (fibonacci 1))

‎examples/std.lbd‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ first: Any = \x: Any. \y: Any. x
1313
second: Any = \x: Any. \y: Any. y
1414

1515
-- misc
16-
-- TODO: Add support for ifzero and cmp
1716
-- TODO: Add support for `use`
1817
float_eq: Float -> Float -> Bool = \x: Float. \y: Float.
1918
(ifzero (cmp x y) true false)

‎include/lbd/intp/builtins.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#pragma once
2+
3+
#include <lbd/intp/interpreter.h>
4+
5+
namespace intp::interp {
6+
std::vector<NativeFunction> get_builtins();
7+
}

‎include/lbd/intp/interpreter.h‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ namespace intp::interp {
2727
const std::vector<std::shared_ptr<Thunk> > &, const std::shared_ptr<Env> &)>;
2828

2929
std::size_t arity;
30+
std::string name;
3031
Impl impl;
3132

3233
friend std::ostream &operator<<(std::ostream &os, const NativeFunction &native_fn);
@@ -40,17 +41,20 @@ namespace intp::interp {
4041
mutable std::optional<Value> cached;
4142
const fe::ast::Expression *expr = nullptr; /// Non-owning, read-only AST pointer
4243
std::shared_ptr<Env> env; /// Environment for evaluating Expression
44+
std::optional<fe::loc::Loc> origin = std::nullopt;
4345

4446
Thunk() = default;
4547

46-
Thunk(const fe::ast::Expression *expr, std::shared_ptr<Env> env);
48+
Thunk(const fe::ast::Expression *expr, std::shared_ptr<Env> env,
49+
std::optional<fe::loc::Loc> origin = std::nullopt);
4750

4851
/// Force computation on Thunk and return a const reference to Value
4952
const Value &force() const; /// Marked const as cached is mutable
5053

5154
/// Sets Thunk's fields after construction
5255
/// Allows for recursive reference
53-
void set(const fe::ast::Expression *expr_, std::shared_ptr<Env> env_);
56+
void set(const fe::ast::Expression *expr_, std::shared_ptr<Env> env_,
57+
std::optional<fe::loc::Loc> origin_ = std::nullopt);
5458
};
5559

5660
struct Env : std::enable_shared_from_this<Env> {
@@ -67,7 +71,7 @@ namespace intp::interp {
6771
Value eval_expr(const fe::ast::Expression &expr, std::shared_ptr<Env> env);
6872

6973
Value apply_fn_apl(Value fn_value, const std::vector<std::shared_ptr<Thunk> > &args,
70-
const std::shared_ptr<Env> &call_site_env);
74+
const std::shared_ptr<Env> &call_site_env, const std::optional<fe::loc::Loc> &call_loc = std::nullopt);
7175

7276
/// Program Driver
7377
struct Result {

‎src/fe/lexer.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ namespace fe::lexer {
6464
return {token::Iden{value}, cur_loc};
6565
}
6666
// Float
67+
// TODO: Negative numbers
6768
if (std::isdigit(c)) {
6869
const size_t start = pos;
6970
while (std::isdigit(c)) {

‎src/intp/buitlins.cpp‎

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
#include <lbd/intp/builtins.h>
2+
3+
namespace intp::interp {
4+
// TODO: If Arity is -1 that means it can consume all the arguments
5+
// Prints Argument to stdout and returns the same Argument as Value
6+
static NativeFunction make_print() {
7+
const std::string name = "print";
8+
return {
9+
1, name, [](const std::vector<std::shared_ptr<Thunk> > &args, const std::shared_ptr<Env> &) -> Value {
10+
const Value &value = args[0]->force();
11+
std::cout << value << std::endl;
12+
return value;
13+
}
14+
};
15+
}
16+
17+
static NativeFunction make_add() {
18+
const std::string name = "add";
19+
return {
20+
2, name, [name](const std::vector<std::shared_ptr<Thunk> > &args, const std::shared_ptr<Env> &) -> Value {
21+
const Value &value1 = args[0]->force();
22+
const Value &value2 = args[1]->force();
23+
if (!std::holds_alternative<double>(value1) || !std::holds_alternative<double>(value2)) {
24+
std::cerr << "runtime error: wrong arguments provided to native function " << name << std::endl
25+
<< name << " signature: Float -> Float -> Float" << std::endl;
26+
exit(EXIT_FAILURE);
27+
}
28+
const double result = std::get<double>(value1) + std::get<double>(value2);
29+
return Value{result};
30+
}
31+
};
32+
}
33+
34+
static NativeFunction make_sub() {
35+
const std::string name = "sub";
36+
return {
37+
2, name, [name](const std::vector<std::shared_ptr<Thunk> > &args, const std::shared_ptr<Env> &) -> Value {
38+
const Value &value1 = args[0]->force();
39+
const Value &value2 = args[1]->force();
40+
if (!std::holds_alternative<double>(value1) || !std::holds_alternative<double>(value2)) {
41+
std::cerr << "runtime error: wrong arguments provided to native function " << name << std::endl
42+
<< name << " signature: Float -> Float -> Float" << std::endl;
43+
exit(EXIT_FAILURE);
44+
}
45+
const double result = std::get<double>(value1) - std::get<double>(value2);
46+
return Value{result};
47+
}
48+
};
49+
}
50+
51+
static NativeFunction make_mul() {
52+
const std::string name = "mul";
53+
return {
54+
2, name, [name](const std::vector<std::shared_ptr<Thunk> > &args, const std::shared_ptr<Env> &) -> Value {
55+
const Value &value1 = args[0]->force();
56+
const Value &value2 = args[1]->force();
57+
if (!std::holds_alternative<double>(value1) || !std::holds_alternative<double>(value2)) {
58+
std::cerr << "runtime error: wrong arguments provided to native function " << name << std::endl
59+
<< name << " signature: Float -> Float -> Float" << std::endl;
60+
exit(EXIT_FAILURE);
61+
}
62+
const double result = std::get<double>(value1) * std::get<double>(value2);
63+
return Value{result};
64+
}
65+
};
66+
}
67+
68+
static NativeFunction make_cmp() {
69+
const std::string name = "cmp";
70+
return {
71+
2, name, [name](const std::vector<std::shared_ptr<Thunk> > &args, const std::shared_ptr<Env> &) -> Value {
72+
const Value &value1 = args[0]->force();
73+
const Value &value2 = args[1]->force();
74+
if (!std::holds_alternative<double>(value1) || !std::holds_alternative<double>(value2)) {
75+
std::cerr << "runtime error: wrong arguments provided to native function " << name << std::endl
76+
<< name << " signature: Float -> Float -> Float" << std::endl;
77+
exit(EXIT_FAILURE);
78+
}
79+
const double num1 = std::get<double>(value1);
80+
const double num2 = std::get<double>(value2);
81+
const int result = (num1 < num2) ? -1 : (num1 > num2 ? 1 : 0);
82+
return Value{static_cast<double>(result)};
83+
}
84+
};
85+
}
86+
87+
static NativeFunction make_if_zero() {
88+
const std::string name = "if_zero";
89+
return {
90+
3, name, [name](const std::vector<std::shared_ptr<Thunk> > &args, const std::shared_ptr<Env> &) -> Value {
91+
const Value &cond_value = args[0]->force();
92+
if (!std::holds_alternative<double>(cond_value)) {
93+
std::cerr << "runtime error: wrong argument type to native function " << name << "\n"
94+
<< name << " signature: Float -> Any -> Any -> Any\n"
95+
<< "runtime error: expected <double> got " << cond_value << std::endl;
96+
exit(EXIT_FAILURE);
97+
}
98+
// Lazy branching: only force the chosen clause
99+
if (const double cond = std::get<double>(cond_value); cond == 0.0) {
100+
return args[1]->force();
101+
}
102+
return args[2]->force();
103+
}
104+
};
105+
}
106+
107+
108+
std::vector<NativeFunction> get_builtins() {
109+
return {
110+
{make_print()},
111+
{make_add()},
112+
{make_sub()},
113+
{make_mul()},
114+
{make_cmp()},
115+
{make_if_zero()}
116+
};
117+
}
118+
}

0 commit comments

Comments
 (0)