Skip to content

Commit 8620a99

Browse files
committed
Added boolean type
1 parent c79a595 commit 8620a99

File tree

6 files changed

+52
-7
lines changed

6 files changed

+52
-7
lines changed

atoms.c

+17
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,35 @@
33
#include <stdlib.h>
44
#include <string.h>
55
#include <stdarg.h>
6+
#include <stdbool.h>
67

8+
static void boolean_writer(void *, Printf);
79
static void number_writer(void *, Printf);
810
static void identifier_writer(void *, Printf);
911
static void string_writer(void *, Printf);
1012
static long last(char *);
1113

1214
void declare_atoms(void) {
15+
boolean_type = declare(free, boolean_writer);
1316
number_type = declare(free, number_writer);
1417
identifier_type = declare(free, identifier_writer);
1518
string_type = declare(free, string_writer);
1619
}
1720

21+
Object *boolean(bool true_or_false) {
22+
bool *cell = (bool *)malloc(sizeof(bool));
23+
*cell = true_or_false;
24+
return wrap(boolean_type, cell);
25+
}
26+
27+
int is_boolean(Object *boolean) {
28+
return is_a(boolean_type, boolean);
29+
}
30+
31+
static void boolean_writer(void *boolean, Printf printer) {
32+
printer("%s", *(bool *)boolean == true ? "true" : "false");
33+
}
34+
1835
Object *number(long number) {
1936
long *cell = (long *)malloc(sizeof(long));
2037
*cell = number;

atoms.h

+4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
#ifndef ATOMS_HEADER
22
#define ATOMS_HEADER
33

4+
#include <stdbool.h>
45
#include "type.h"
56

7+
Type *boolean_type;
68
Type *number_type;
79
Type *identifier_type;
810
Type *string_type;
911

1012
void declare_atoms(void);
13+
Object *boolean(bool);
14+
int is_boolean(Object *);
1115
Object *number(long);
1216
int is_number(Object *);
1317
Object *identifier(char *);

function.c

+12-5
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@ struct Lambda_ {
1414

1515
static void built_in_writer(void *, Printf);
1616
static void lambda_writer(void *, Printf);
17+
static void destroy_lambda(void *);
1718

1819
void declare_functions(void) {
1920
built_in_type = declare(free, built_in_writer);
20-
lambda_type = declare(free, lambda_writer);
21+
lambda_type = declare(destroy_lambda, lambda_writer);
2122
}
2223

2324
Object *built_in(Callable code) {
@@ -39,14 +40,14 @@ Callable code(BuiltIn *built_in) {
3940
return built_in->code;
4041
}
4142

42-
Object *body(Object *function) {
43-
return ((Lambda *)value(function))->body;
44-
}
45-
4643
Object *parameters(Object *function) {
4744
return ((Lambda *)value(function))->parameters;
4845
}
4946

47+
Object *body(Object *function) {
48+
return ((Lambda *)value(function))->body;
49+
}
50+
5051
Object *special_form(Object *function) {
5152
if (is_built_in(function)) {
5253
((BuiltIn *)value(function))->special_form = 1;
@@ -87,3 +88,9 @@ static void lambda_writer(void *lambda, Printf printer) {
8788
write_object(((Lambda *)lambda)->body, printer);
8889
printer(">");
8990
}
91+
92+
static void destroy_lambda(void *lambda) {
93+
destroy(((Lambda *)lambda)->parameters);
94+
destroy(((Lambda *)lambda)->body);
95+
free(lambda);
96+
}

function.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ void declare_functions(void);
1515
Object *built_in(Callable);
1616
Object *lambda(Object *, Object *);
1717
Callable code(BuiltIn *built_in);
18-
Object *body(Object *);
1918
Object *parameters(Object *);
19+
Object *body(Object *);
2020
Object *special_form(Object *);
2121
int is_function(Object *);
2222
int is_special_form(Object *);

parser.l

+11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
%{
22

3+
#include <stdbool.h>
34
#include "y.tab.h"
45

56
%}
@@ -10,6 +11,16 @@
1011
return NIL;
1112
}
1213

14+
true {
15+
yylval.b = true;
16+
return BOOLEAN;
17+
}
18+
19+
false {
20+
yylval.b = false;
21+
return BOOLEAN;
22+
}
23+
1324
[0-9]+ {
1425
yylval.l = atoi(yytext);
1526
return NUMBER;

parser.y

+7-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "stack.h"
88
#include <stdlib.h>
99
#include <stdio.h>
10+
#include <stdbool.h>
1011

1112
Stack *input = NULL;
1213

@@ -52,11 +53,13 @@ static Object *pour_stack_into_list(Object *list, Stack *stack) {
5253
%}
5354

5455
%token NIL
56+
%token <b> BOOLEAN
5557
%token <l> NUMBER
5658
%token <s> IDENTIFIER
5759
%token <s> QUOTED_STRING
5860

5961
%union {
62+
bool b;
6063
long l;
6164
char *s;
6265
}
@@ -102,7 +105,10 @@ elements: element | elements element;
102105
element: atom
103106
| list;
104107

105-
atom: NUMBER {
108+
atom: BOOLEAN {
109+
push((Stack *)peek(input), (void *)boolean($1));
110+
}
111+
| NUMBER {
106112
push((Stack *)peek(input), (void *)number($1));
107113
}
108114
| IDENTIFIER {

0 commit comments

Comments
 (0)