Skip to content

Commit d5ad366

Browse files
committed
Add README section about global vars, rename some predefined fns
1 parent 8061a5a commit d5ad366

File tree

7 files changed

+64
-45
lines changed

7 files changed

+64
-45
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,25 @@ nil
8282
3
8383
```
8484

85+
### Global variables
86+
87+
```
88+
>>> (defvar x 100)
89+
nil
90+
>>> x
91+
100
92+
93+
94+
>>> (defonce y 200)
95+
nil
96+
>>> y
97+
200
98+
>>> (defonce y 300)
99+
nil
100+
>>> y
101+
200
102+
```
103+
85104
### "Standard library"
86105

87106
It is located in file [`stdlib.unl`](https://github.com/OlegTheCat/unlisp-llvm/blob/master/stdlib.unl).

stdlib.unl

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,51 @@
1-
(declare-sym true)
2-
(set-val (quote true) (quote true))
1+
(declare-var true)
2+
(set-symbol-value (quote true) (quote true))
33

4-
(set-fn (quote list) (lambda (& args) args))
4+
(set-symbol-function (quote list) (lambda (& args) args))
55

6-
(set-fn
6+
(set-symbol-function
77
(quote list*)
88
(lambda list* (& args)
99
(apply (symbol-function (quote apply))
1010
(symbol-function (quote list)) args)))
1111

12-
(set-fn
12+
(set-symbol-function
1313
(quote funcall)
1414
(lambda funcall (f & args)
1515
(apply f args)))
1616

17-
(set-fn
17+
(set-symbol-function
1818
(quote emptyp)
1919
(lambda (list) (if list nil true)))
2020

21-
(set-fn
21+
(set-symbol-function
2222
(quote append)
2323
(lambda append (x y)
2424
(if (emptyp x)
2525
y
2626
(cons (first x)
2727
(append (rest x) y)))))
2828

29-
(set-fn
29+
(set-symbol-function
3030
(quote list*)
3131
(lambda list* (& args)
3232
(apply (symbol-function (quote apply))
3333
(symbol-function (quote list)) args)))
3434

35-
(set-fn
35+
(set-symbol-function
3636
(quote funcall)
3737
(lambda funcall (f & args)
3838
(apply f args)))
3939

40-
(set-fn
40+
(set-symbol-function
4141
(quote append)
4242
(lambda append (x y)
4343
(if (emptyp x)
4444
y
4545
(cons (first x)
4646
(append (rest x) y)))))
4747

48-
(set-fn
48+
(set-symbol-function
4949
(quote reduce)
5050
(lambda reduce (f init list)
5151
(if (emptyp list)
@@ -56,7 +56,7 @@
5656
(rest list)))))
5757

5858

59-
(set-fn
59+
(set-symbol-function
6060
(quote reverse-inner)
6161
(lambda (x acc)
6262
(if (emptyp x)
@@ -65,39 +65,39 @@
6565
(rest x)
6666
(cons (first x) acc)))))
6767

68-
(set-fn
68+
(set-symbol-function
6969
(quote reverse)
7070
(lambda reverse (x)
7171
(reverse-inner x ())))
7272

73-
(set-fn
73+
(set-symbol-function
7474
(quote not)
7575
(lambda not (x)
76-
(if x nil (quote true))))
76+
(if x nil true)))
7777

7878

79-
(set-fn
79+
(set-symbol-function
8080
(quote unq)
8181
(lambda unq (x)
8282
(error "unq outside of qquote")))
8383

8484
(set-macro (symbol-function (quote unq)))
8585

86-
(set-fn
86+
(set-symbol-function
8787
(quote unqs)
8888
(lambda unqs (x)
8989
(error "unqs outside of qquote")))
9090

9191
(set-macro (symbol-function (quote unqs)))
9292

93-
(set-fn
93+
(set-symbol-function
9494
(quote qquote)
9595
(lambda quote (x)
9696
(qquote-process x)))
9797

9898
(set-macro (symbol-function (quote qquote)))
9999

100-
(set-fn
100+
(set-symbol-function
101101
(quote qquote-process)
102102
(lambda qquote-process (x)
103103
(if (not (listp x))
@@ -112,12 +112,12 @@
112112
(error "unqs after qquote")
113113
(qquote-transform-list x))))))))
114114

115-
(set-fn
115+
(set-symbol-function
116116
(quote qquote-transform-list)
117117
(lambda qquote-transform-list (x)
118118
(qquote-transform-list-inner x ())))
119119

120-
(set-fn
120+
(set-symbol-function
121121
(quote qquote-transform-list-inner)
122122
(lambda qquote-transform-list-inner (x transformed-acc)
123123
(if (emptyp x)
@@ -129,7 +129,7 @@
129129
(cons (qquote-transform-list-item (first x))
130130
transformed-acc)))))
131131

132-
(set-fn
132+
(set-symbol-function
133133
(quote qquote-transform-list-item)
134134
(lambda qquote-transform-list-item (x)
135135
(if (not (listp x))
@@ -142,13 +142,13 @@
142142
(first (rest x))
143143
(list (quote list) (qquote-process x))))))))
144144

145-
(set-fn
145+
(set-symbol-function
146146
(quote defmacro)
147147
(lambda defmacro (name args & body)
148148
(qquote
149149
(let ((--macro-fn (lambda (unq name) (unq args)
150150
(unqs body))))
151-
(set-fn
151+
(set-symbol-function
152152
(quote (unq name))
153153
--macro-fn)
154154
(set-macro --macro-fn)))))
@@ -157,7 +157,7 @@
157157

158158
(defmacro defun (name args & body)
159159
(qquote
160-
(set-fn
160+
(set-symbol-function
161161
(quote (unq name))
162162
(lambda (unq name) (unq args)
163163
(unqs body)))))
@@ -200,7 +200,7 @@
200200

201201
(defmacro and (& forms)
202202
(let ((reversed (if (emptyp forms)
203-
(list (quote true))
203+
(list true)
204204
(reverse forms))))
205205
(reduce
206206
(lambda (acc form)
@@ -310,8 +310,8 @@
310310
(defmacro defvar (sym val)
311311
(qquote
312312
(let ()
313-
(declare-sym (unq sym))
314-
(set-val (quote (unq sym))
313+
(declare-var (unq sym))
314+
(set-symbol-value (quote (unq sym))
315315
(unq val)))))
316316

317317
(defmacro defonce (sym val)

unlisp_rt/src/predefined.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ pub fn init() {
430430
init_symbol_fn(
431431
native_set_fn_invoke as *const c_void,
432432
native_set_fn_apply as *const c_void,
433-
"set-fn",
433+
"set-symbol-function",
434434
&["sym", "func"],
435435
false,
436436
);
@@ -540,7 +540,7 @@ pub fn init() {
540540
init_symbol_fn(
541541
native_set_val_invoke as *const c_void,
542542
native_set_val_apply as *const c_void,
543-
"set-val",
543+
"set-symbol-value",
544544
&["sym", "val"],
545545
false,
546546
);

unlispc/src/codegen/closure.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use super::top_level::compile_hirs;
1313
use std::iter;
1414

1515
fn is_global_name(ctx: &CodegenContext, name: &String) -> bool {
16-
ctx.lookup_local_name(name).is_none() && ctx.is_global_sym(name)
16+
ctx.lookup_local_name(name).is_none() && ctx.is_global_var(name)
1717
}
1818

1919
fn codegen_raw_fn(ctx: &mut CodegenContext, closure: &Closure) -> GenResult<FunctionValue> {

unlispc/src/codegen/context.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,11 @@ impl<'a> CodegenContext<'a> {
107107
self.str_literal_globals = HashMap::new();
108108
}
109109

110-
pub fn declare_global_sym(&mut self, name: &String) {
110+
pub fn declare_global_var(&mut self, name: &String) {
111111
self.declared_syms.insert(name.clone());
112112
}
113113

114-
pub fn is_global_sym(&self, name: &String) -> bool {
114+
pub fn is_global_var(&self, name: &String) -> bool {
115115
self.declared_syms.get(name).is_some()
116116
}
117117

unlispc/src/codegen/top_level.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ pub fn compile_hir(ctx: &mut CodegenContext, hir: &HIR) -> CompileResult {
2020
HIR::If(if_hir) => compile_if(ctx, if_hir),
2121
HIR::Quote(quote) => compile_quoted_literal(ctx, &quote.body),
2222
HIR::LetBlock(let_block) => compile_let_block(ctx, let_block),
23-
HIR::DeclareSym(decl_s) => {
24-
ctx.declare_global_sym(&decl_s.var_name);
23+
HIR::DeclareVar(decl_var) => {
24+
ctx.declare_global_var(&decl_var.var_name);
2525
Ok(compile_nil_literal(ctx))
2626
}
2727
}

unlispc/src/repr.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ pub struct Call {
7272
}
7373

7474
#[derive(Debug, PartialEq, Eq, Clone)]
75-
pub struct DeclareSym {
75+
pub struct DeclareVar {
7676
pub var_name: String,
7777
}
7878

@@ -87,7 +87,7 @@ pub enum Literal {
8787

8888
#[derive(Debug, PartialEq, Eq, Clone)]
8989
pub enum HIR {
90-
DeclareSym(DeclareSym),
90+
DeclareVar(DeclareVar),
9191
Literal(Literal),
9292
Lambda(Lambda),
9393
Closure(Closure),
@@ -312,25 +312,25 @@ fn forms_to_hir(forms: &Vec<Form>) -> Result<HIR, Box<dyn Error>> {
312312

313313
Ok(HIR::Lambda(lambda))
314314
}
315-
Form::Symbol(s) if is(s, "declare-sym") => {
315+
Form::Symbol(s) if is(s, "declare-var") => {
316316
let sym = forms
317317
.get(1)
318-
.ok_or_else(|| error::Error::new_syntax_error("no symbol in declare-sym"))?;
318+
.ok_or_else(|| error::Error::new_syntax_error("no symbol in declare-var"))?;
319319
let sym = to_symbol(sym)
320-
.ok_or_else(|| error::Error::new_syntax_error("not a symbol in declare-sym"))?;
320+
.ok_or_else(|| error::Error::new_syntax_error("not a symbol in declare-var"))?;
321321

322322
if forms.get(2).is_some() {
323323
Err(error::Error::new_syntax_error(format!(
324-
"wrong number of arguments ({}) passed to declare-sym",
324+
"wrong number of arguments ({}) passed to declare-var",
325325
forms.len() - 1
326326
)))?
327327
}
328328

329-
let decl_s = DeclareSym {
329+
let decl_var = DeclareVar {
330330
var_name: sym.clone(),
331331
};
332332

333-
Ok(HIR::DeclareSym(decl_s))
333+
Ok(HIR::DeclareVar(decl_var))
334334
}
335335
Form::Symbol(s) => unsafe {
336336
let call_sym = symbols::get_or_intern_symbol(s.clone());
@@ -480,7 +480,7 @@ fn convert_lambda_body_item(
480480

481481
HIR::If(converted)
482482
}
483-
HIR::DeclareSym(decl_s) => HIR::DeclareSym(decl_s.clone())
483+
HIR::DeclareVar(decl_var) => HIR::DeclareVar(decl_var.clone()),
484484
}
485485
}
486486

@@ -550,7 +550,7 @@ pub fn convert_into_closures(hir: &HIR) -> HIR {
550550
HIR::If(converted)
551551
}
552552

553-
HIR::DeclareSym(decl_s) => HIR::DeclareSym(decl_s.clone())
553+
HIR::DeclareVar(decl_var) => HIR::DeclareVar(decl_var.clone()),
554554
}
555555
}
556556

0 commit comments

Comments
 (0)