on master help sign in
efdc9fciko
nock/Cargo.toml+2−1
@@ -38,7 +38,8 @@ wasm-bindgen-test = "0.3.34"
38
38
39[profile.release]
39[profile.release]
40# Tell `rustc` to optimize for small code size.
40# Tell `rustc` to optimize for small code size.
41−opt-level = "s"
41+opt-level = 3
42+debug = true
42
43
43[[bin]]
44[[bin]]
44name = "nock"
45name = "nock"
nock/NOTES.mdnew file+3
@@ -0,0 +1,3 @@
1+```
2+.mint/nock |=(gen=hoon (~(mint ut %noun) %noun gen))
3+```
nock/res/hoon-hoon-ast.nocknew file

Binary file not shown.

nock/res/mint.nocknew file

Binary file not shown.

nock/res/ream.nocknew file

Binary file not shown.

nock/src/interpreter.rs+49−56
@@ -89,7 +89,7 @@ fn tis(ctx: &InterpreterContext, lhs: Rc<Noun>, rhs: Rc<Noun>) -> Rc<Noun> {
89 }
89 }
90}
90}
91
91
92−fn fas(ctx: &InterpreterContext, addr: Atom, noun: Rc<Noun>) -> Option<Rc<Noun>> {
92+fn fas(ctx: &InterpreterContext, addr: Atom, noun: &Noun) -> Option<Rc<Noun>> {
93 let mut addr_iter = addr.iter_u32_digits();
93 let mut addr_iter = addr.iter_u32_digits();
94
94
95 match addr_iter.len() {
95 match addr_iter.len() {
@@ -103,24 +103,24 @@ fn fas(ctx: &InterpreterContext, addr: Atom, noun: Rc<Noun>) -> Option<Rc<Noun>>
103 } else {
103 } else {
104 2
104 2
105 },
105 },
106− rest,
106+ &rest,
107 )?)
107 )?)
108 }
108 }
109 }
109 }
110}
110}
111
111
112−fn fas_u32(addr: u32, noun: Rc<Noun>) -> Option<Rc<Noun>> {
112+fn fas_u32(addr: u32, noun: &Noun) -> Option<Rc<Noun>> {
113 match addr {
113 match addr {
114 0 => None,
114 0 => None,
115− 1 => Some(noun),
115+ 1 => Some(Rc::new(noun.clone())),
116− n => match (*noun).clone() {
116+ n => match noun {
117 Noun::Atom(_) => None,
117 Noun::Atom(_) => None,
118 Noun::Cell { p, q, .. } => match n {
118 Noun::Cell { p, q, .. } => match n {
119− 2 => Some(p),
119+ 2 => Some(p.clone()),
120− 3 => Some(q),
120+ 3 => Some(q.clone()),
121 _ => {
121 _ => {
122− let rest = fas_u32(n >> 1, cell(p, q))?;
122+ let rest = fas_u32(n >> 1, &naked_cell(p.clone(), q.clone()))?;
123− fas_u32(if (n & 1) == 1 { 3 } else { 2 }, rest)
123+ fas_u32(if (n & 1) == 1 { 3 } else { 2 }, &rest)
124 }
124 }
125 },
125 },
126 },
126 },
@@ -134,9 +134,9 @@ fn hax_u32(addr: u32, new_value: Rc<Noun>, target: Rc<Noun>) -> Option<Rc<Noun>>
134 _ => hax_u32(
134 _ => hax_u32(
135 addr >> 1,
135 addr >> 1,
136 if (addr & 1) == 1 {
136 if (addr & 1) == 1 {
137− cell(fas_u32(addr - 1, target.clone())?, new_value)
137+ cell(fas_u32(addr - 1, &target)?, new_value)
138 } else {
138 } else {
139− cell(new_value, fas_u32(addr + 1, target.clone())?)
139+ cell(new_value, fas_u32(addr + 1, &target)?)
140 },
140 },
141 target,
141 target,
142 ),
142 ),
@@ -158,27 +158,16 @@ fn hax(
158 ctx,
158 ctx,
159 &addr >> 1u32,
159 &addr >> 1u32,
160 if (&addr & &ctx.big_uints.one) == ctx.big_uints.one {
160 if (&addr & &ctx.big_uints.one) == ctx.big_uints.one {
161− cell(
162− fas(ctx, addr - &ctx.big_uints.one, target.clone())?,
161+ cell(fas(ctx, addr - &ctx.big_uints.one, &target)?, new_value)
163− new_value,
164− )
165 } else {
162 } else {
166− cell(
167− new_value,
168− fas(ctx, addr + &ctx.big_uints.one, target.clone())?,
163+ cell(new_value, fas(ctx, addr + &ctx.big_uints.one, &target)?)
169− )
170 },
164 },
171 target,
165 target,
172 ),
166 ),
173 }
167 }
174}
168}
175
169
176−fn tar_u32(
177− ctx: &InterpreterContext,
170+fn tar_u32(ctx: &InterpreterContext, subj: &Noun, op: u32, formula: Rc<Noun>) -> Option<Rc<Noun>> {
178− subj: Rc<Noun>,
179− op: u32,
180− formula: Rc<Noun>,
181−) -> Option<Rc<Noun>> {
182 match op {
171 match op {
183 0 => {
172 0 => {
184 let b = formula.as_atom()?.clone();
173 let b = formula.as_atom()?.clone();
@@ -203,53 +192,62 @@ fn tar_u32(
203 let (hash, sample_1, sample_2) = subj.hash_double_gate();
192 let (hash, sample_1, sample_2) = subj.hash_double_gate();
204 match ctx.double_jets.get(&hash) {
193 match ctx.double_jets.get(&hash) {
205 Some(f) => f(ctx, sample_1, sample_2),
194 Some(f) => f(ctx, sample_1, sample_2),
195+ None => {
206− None => tar(ctx, tar(ctx, subj.clone(), b)?, tar(ctx, subj, c)?),
196+ let foo = tar(ctx, subj, &b)?;
197+ let bar = tar(ctx, subj, &c)?;
198+ tar(ctx, &foo, &bar)
199+ }
207 }
200 }
208 }
201 }
209 }
202 }
210 }
203 }
204+ _ => {
211− _ => tar(ctx, tar(ctx, subj.clone(), b)?, tar(ctx, subj, c)?),
205+ let foo = tar(ctx, &subj, &b)?;
206+ let bar = tar(ctx, &subj, &c)?;
207+ tar(ctx, &foo, &bar)
208+ }
212 }
209 }
213 }
210 }
214− 3 => Some(wut(ctx, tar(ctx, subj, formula)?)),
211+ 3 => Some(wut(ctx, tar(ctx, subj, &formula)?)),
215− 4 => Some(Rc::new(lus(tar(ctx, subj, formula)?)?)),
212+ 4 => Some(Rc::new(lus(tar(ctx, subj, &formula)?)?)),
216 5 => {
213 5 => {
217 let (b, c) = formula.as_cell()?;
214 let (b, c) = formula.as_cell()?;
215+ let foo = tar(ctx, &subj, &b)?;
218− Some(tis(ctx, tar(ctx, subj.clone(), b)?, tar(ctx, subj, c)?))
216+ Some(tis(ctx, foo, tar(ctx, subj, &c)?))
219 }
217 }
220 6 => {
218 6 => {
221 let (b, c) = formula.as_cell()?;
219 let (b, c) = formula.as_cell()?;
222 let (c, d) = c.as_cell()?;
220 let (c, d) = c.as_cell()?;
223
221
224− tar(
222+ let foo = tar_u32(
225 ctx,
223 ctx,
226− subj.clone(),
224+ &cell(c, d),
225+ 0,
227 tar_u32(
226 tar_u32(
228 ctx,
227 ctx,
229− cell(c, d),
228+ &ctx.nouns.two_three,
230 0,
229 0,
231− tar_u32(
232− ctx,
233− ctx.nouns.two_three.clone(),
234− 0,
235− tar_u32(ctx, subj, 4, cell(ctx.nouns.four.clone(), b))?,
230+ tar_u32(ctx, subj, 4, cell(ctx.nouns.four.clone(), b))?,
236− )?,
237 )?,
231 )?,
238− )
232+ )?;
233+
234+ tar(ctx, &subj, &foo)
239 }
235 }
240 7 => {
236 7 => {
241 let (b, c) = formula.as_cell()?;
237 let (b, c) = formula.as_cell()?;
242− tar(ctx, tar(ctx, subj, b)?, c)
238+ let foo = tar(ctx, subj, &b)?;
239+ tar(ctx, &foo, &c)
243 }
240 }
244 8 => {
241 8 => {
245 let (b, c) = formula.as_cell()?;
242 let (b, c) = formula.as_cell()?;
246− tar(ctx, cell(tar(ctx, subj.clone(), b)?, subj), c)
243+ tar(ctx, &cell(tar(ctx, &subj, &b)?, Rc::new(subj.clone())), &c)
247 }
244 }
248 9 => {
245 9 => {
249 let (b, c) = formula.as_cell()?;
246 let (b, c) = formula.as_cell()?;
247+ let foo = tar(ctx, &subj, &c)?;
250 tar_u32(
248 tar_u32(
251 ctx,
249 ctx,
252− tar(ctx, subj, c)?,
250+ &foo,
253 2,
251 2,
254 cell(ctx.nouns.sig_one.clone(), cell(ctx.nouns.sig.clone(), b)),
252 cell(ctx.nouns.sig_one.clone(), cell(ctx.nouns.sig.clone(), b)),
255 )
253 )
@@ -259,21 +257,16 @@ fn tar_u32(
259 let (b, c) = b.as_cell()?;
257 let (b, c) = b.as_cell()?;
260 let b = b.as_atom()?;
258 let b = b.as_atom()?;
261
259
262− hax(
263− ctx,
264− b.clone(),
265− tar(ctx, subj.clone(), c)?,
266− tar(ctx, subj, d)?,
260+ hax(ctx, b.clone(), tar(ctx, subj, &c)?, tar(ctx, subj, &d)?)
267− )
268 }
261 }
269 11 => {
262 11 => {
270 let (b, d) = formula.as_cell()?;
263 let (b, d) = formula.as_cell()?;
271 // println!("{b}");
264 // println!("{b}");
272 match (*b).clone() {
265 match (*b).clone() {
273− Noun::Atom(_) => tar(ctx, subj, d),
266+ Noun::Atom(_) => tar(ctx, subj, &d),
274 Noun::Cell { q: c, .. } => tar_u32(
267 Noun::Cell { q: c, .. } => tar_u32(
275 ctx,
268 ctx,
276− cell(tar(ctx, subj.clone(), c)?, tar(ctx, subj, d)?),
269+ &cell(tar(ctx, subj, &c)?, tar(ctx, subj, &d)?),
277 0,
270 0,
278 ctx.nouns.three.clone(),
271 ctx.nouns.three.clone(),
279 ),
272 ),
@@ -283,10 +276,10 @@ fn tar_u32(
283 }
276 }
284}
277}
285
278
286−pub fn tar(ctx: &InterpreterContext, subj: Rc<Noun>, formula: Rc<Noun>) -> Option<Rc<Noun>> {
279+pub fn tar(ctx: &InterpreterContext, subj: &Noun, formula: &Noun) -> Option<Rc<Noun>> {
287 let (op, formula) = formula.as_cell()?;
280 let (op, formula) = formula.as_cell()?;
288 match *op {
281 match *op {
289− Noun::Cell { .. } => Some(cell(tar(ctx, subj.clone(), op)?, tar(ctx, subj, formula)?)),
282+ Noun::Cell { .. } => Some(cell(tar(ctx, subj, &op)?, tar(ctx, subj, &formula)?)),
290 Noun::Atom(ref op) => {
283 Noun::Atom(ref op) => {
291 let mut op_iter = op.iter_u32_digits();
284 let mut op_iter = op.iter_u32_digits();
292 let op = match op_iter.len() {
285 let op = match op_iter.len() {
@@ -295,7 +288,7 @@ pub fn tar(ctx: &InterpreterContext, subj: Rc<Noun>, formula: Rc<Noun>) -> Optio
295 _ => None,
288 _ => None,
296 };
289 };
297 let op = op?;
290 let op = op?;
298− stacker::maybe_grow(32 * 1024, 1024 * 1024, || tar_u32(ctx, subj, op, formula))
291+ stacker::maybe_grow(32 * 1024, 1024 * 1024, || tar_u32(ctx, &subj, op, formula))
299 }
292 }
300 }
293 }
301}
294}
@@ -303,8 +296,8 @@ pub fn tar(ctx: &InterpreterContext, subj: Rc<Noun>, formula: Rc<Noun>) -> Optio
303pub fn eval_gate(ctx: &InterpreterContext, gate: Rc<Noun>) -> Option<Rc<Noun>> {
296pub fn eval_gate(ctx: &InterpreterContext, gate: Rc<Noun>) -> Option<Rc<Noun>> {
304 tar(
297 tar(
305 ctx,
298 ctx,
306− gate,
299+ &gate,
307− cell(
300+ &cell(
308 Rc::new(Noun::Atom(BigUint::new(vec![9]))),
301 Rc::new(Noun::Atom(BigUint::new(vec![9]))),
309 cell(
302 cell(
310 ctx.nouns.two.clone(),
303 ctx.nouns.two.clone(),
nock/src/jets.rs+14−1
@@ -27,7 +27,10 @@ pub type Jets = BTreeMap<noun::Hash, Jet>;
27pub type DoubleJets = BTreeMap<noun::Hash, DoubleJet>;
27pub type DoubleJets = BTreeMap<noun::Hash, DoubleJet>;
28
28
29pub fn generate_double_jets() -> DoubleJets {
29pub fn generate_double_jets() -> DoubleJets {
30+ BTreeMap::from([
30− BTreeMap::from([(210326504639957723220404138306543145797, JUST)])
31+ (210326504639957723220404138306543145797, JUST),
32+ // (255213039400865122956425872354360738044, STIR),
33+ ])
31}
34}
32
35
33pub fn generate_jets() -> Jets {
36pub fn generate_jets() -> Jets {
@@ -960,6 +963,16 @@ static JUST: DoubleJet = |ctx, n, m| {
960 }
963 }
961};
964};
962
965
966+// static STIR: DoubleJet = |ctx, n, m| {
967+// let (rud, raq) = n.as_cell()?;
968+// let (raq, fel) = raq.as_cell()?;
969+// let tub = m.as_nail()?;
970+
971+// let mut lef = Vec::new();
972+// let mut vex = slam(ctx, fel, tub)?.as;
973+// if vex.
974+// };
975+
963fn next(nail: Nail) -> Option<Edge> {
976fn next(nail: Nail) -> Option<Edge> {
964 if *nail.rest == Noun::SIG {
977 if *nail.rest == Noun::SIG {
965 Some(fail(nail))
978 Some(fail(nail))
nock/src/main.rs+28−16
@@ -4,7 +4,7 @@ use nock::{
4 cue::cue_bytes,
4 cue::cue_bytes,
5 interpreter::{self, eval_gate, generate_interpreter_context, slam},
5 interpreter::{self, eval_gate, generate_interpreter_context, slam},
6 jam::jam_to_bytes,
6 jam::jam_to_bytes,
7− noun::{Atom, Noun},
7+ noun::{cell, Atom, Noun},
8};
8};
9use spinoff::Spinner;
9use spinoff::Spinner;
10use std::{
10use std::{
@@ -149,28 +149,40 @@ fn new_spinner(txt: String) -> Spinner {
149}
149}
150
150
151fn build(root: PathBuf, output: PathBuf) -> Result<(), std::io::Error> {
151fn build(root: PathBuf, output: PathBuf) -> Result<(), std::io::Error> {
152+ println!("reaming");
153+ let root_ast = {
152− let make_nock = include_bytes!("../res/make.nock");
154+ let ream_nock = include_bytes!("../res/ream.nock");
153− let make = cue_bytes(make_nock);
155+ let ream_gate = cue_bytes(ream_nock);
156+
157+ let root_source = read_file(&root)?;
158+
159+ slam(
160+ &generate_interpreter_context(),
161+ ream_gate,
162+ Rc::new(Noun::Atom(Atom::from_bytes_le(&root_source))),
163+ )
164+ .unwrap()
165+ };
154
166
155− let root = read_file(&root)?;
167+ println!("Loading hoon/hoon");
156
168
169+ let ast = {
157− let hoon_hoon = include_bytes!("../res/hoon.hoon");
170+ let hoon_hoon_ast_nock = include_bytes!("../res/hoon-hoon-ast.nock");
171+ let hoon_hoon_ast = cue_bytes(hoon_hoon_ast_nock);
172+ cell(
173+ Rc::new(Noun::Atom(Atom::from_bytes_le(b"tsgl"))),
174+ cell(hoon_hoon_ast, root_ast),
175+ )
176+ };
158
177
159− let mut source = Vec::new();
178+ println!("minting");
160
179
161− source.extend_from_slice(b"=>\n");
180+ let mint_gate = cue_bytes(include_bytes!("../res/mint.nock"));
162− source.extend_from_slice(hoon_hoon);
181+ let slam_result = slam(&generate_interpreter_context(), mint_gate, ast).unwrap();
163− source.extend_from_slice(&root);
164
182
165− let slam_result = slam(
183+ // println!("{slam_result}");
166− &generate_interpreter_context(),
167− make,
168− Rc::new(Noun::Atom(Atom::from_bytes_le(&source))),
169− )
170− .unwrap();
171
184
172 let mut output_file = File::create(output)?;
185 let mut output_file = File::create(output)?;
173 File::write_all(&mut output_file, &jam_to_bytes(slam_result)).unwrap();
186 File::write_all(&mut output_file, &jam_to_bytes(slam_result)).unwrap();
174 Ok(())
187 Ok(())
175}
188}
176−// [%7 <hoon/hoon> <make-result>]
nock/src/noun.rs+6−2
@@ -400,12 +400,16 @@ fn print_noun(noun: Rc<Noun>, is_rhs: bool) -> String {
400}
400}
401
401
402pub fn cell(p: Rc<Noun>, q: Rc<Noun>) -> Rc<Noun> {
402pub fn cell(p: Rc<Noun>, q: Rc<Noun>) -> Rc<Noun> {
403+ Rc::new(naked_cell(p, q))
404+}
405+
406+pub fn naked_cell(p: Rc<Noun>, q: Rc<Noun>) -> Noun {
403− Rc::new(Noun::Cell {
407+ Noun::Cell {
404 p: p.clone(),
408 p: p.clone(),
405 q: q.clone(),
409 q: q.clone(),
406 hash: Cell::new(None),
410 hash: Cell::new(None),
407 compiled_gate: None,
411 compiled_gate: None,
408− })
412+ }
409}
413}
410
414
411fn hash_pair(p: Rc<Noun>, q: Rc<Noun>) -> Hash {
415fn hash_pair(p: Rc<Noun>, q: Rc<Noun>) -> Hash {