on master help sign in
commitefdc9fc855c72e492382b1327fd62ce7ee160e16 authoriko <mail@iko.soy> dateSat 31 Aug 2024, 07:23 (2 years ago) parent689bf72
9 files changed+102−76
nock/Cargo.toml+2−1 nock/NOTES.md+3 nock/res/hoon-hoon-ast.nock nock/res/mint.nock nock/res/ream.nock nock/src/interpreter.rs+49−56 nock/src/jets.rs+14−1 nock/src/main.rs+28−16 nock/src/noun.rs+6−2
nock/Cargo.toml+2−1
@@ -38,7 +38,8 @@ wasm-bindgen-test = "0.3.34"
3838
3939[profile.release]
4040# Tell `rustc` to optimize for small code size.
41−opt-level = "s"
41+opt-level = 3
42+debug = true
4243
4344[[bin]]
4445name = "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> {
8989 }
9090}
9191
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>> {
9393 let mut addr_iter = addr.iter_u32_digits();
9494
9595 match addr_iter.len() {
@@ -103,24 +103,24 @@ fn fas(ctx: &InterpreterContext, addr: Atom, noun: Rc<Noun>) -> Option<Rc<Noun>>
103103 } else {
104104 2
105105 },
106− rest,
106+ &rest,
107107 )?)
108108 }
109109 }
110110}
111111
112−fn fas_u32(addr: u32, noun: Rc<Noun>) -> Option<Rc<Noun>> {
112+fn fas_u32(addr: u32, noun: &Noun) -> Option<Rc<Noun>> {
113113 match addr {
114114 0 => None,
115− 1 => Some(noun),
116− n => match (*noun).clone() {
115+ 1 => Some(Rc::new(noun.clone())),
116+ n => match noun {
117117 Noun::Atom(_) => None,
118118 Noun::Cell { p, q, .. } => match n {
119− 2 => Some(p),
120− 3 => Some(q),
119+ 2 => Some(p.clone()),
120+ 3 => Some(q.clone()),
121121 _ => {
122− let rest = fas_u32(n >> 1, cell(p, q))?;
123− fas_u32(if (n & 1) == 1 { 3 } else { 2 }, rest)
122+ let rest = fas_u32(n >> 1, &naked_cell(p.clone(), q.clone()))?;
123+ fas_u32(if (n & 1) == 1 { 3 } else { 2 }, &rest)
124124 }
125125 },
126126 },
@@ -134,9 +134,9 @@ fn hax_u32(addr: u32, new_value: Rc<Noun>, target: Rc<Noun>) -> Option<Rc<Noun>>
134134 _ => hax_u32(
135135 addr >> 1,
136136 if (addr & 1) == 1 {
137− cell(fas_u32(addr - 1, target.clone())?, new_value)
137+ cell(fas_u32(addr - 1, &target)?, new_value)
138138 } else {
139− cell(new_value, fas_u32(addr + 1, target.clone())?)
139+ cell(new_value, fas_u32(addr + 1, &target)?)
140140 },
141141 target,
142142 ),
@@ -158,27 +158,16 @@ fn hax(
158158 ctx,
159159 &addr >> 1u32,
160160 if (&addr & &ctx.big_uints.one) == ctx.big_uints.one {
161− cell(
162− fas(ctx, addr - &ctx.big_uints.one, target.clone())?,
163− new_value,
164− )
161+ cell(fas(ctx, addr - &ctx.big_uints.one, &target)?, new_value)
165162 } else {
166− cell(
167− new_value,
168− fas(ctx, addr + &ctx.big_uints.one, target.clone())?,
169− )
163+ cell(new_value, fas(ctx, addr + &ctx.big_uints.one, &target)?)
170164 },
171165 target,
172166 ),
173167 }
174168}
175169
176−fn tar_u32(
177− ctx: &InterpreterContext,
178− subj: Rc<Noun>,
179− op: u32,
180− formula: Rc<Noun>,
181−) -> Option<Rc<Noun>> {
170+fn tar_u32(ctx: &InterpreterContext, subj: &Noun, op: u32, formula: Rc<Noun>) -> Option<Rc<Noun>> {
182171 match op {
183172 0 => {
184173 let b = formula.as_atom()?.clone();
@@ -203,53 +192,62 @@ fn tar_u32(
203192 let (hash, sample_1, sample_2) = subj.hash_double_gate();
204193 match ctx.double_jets.get(&hash) {
205194 Some(f) => f(ctx, sample_1, sample_2),
206− None => tar(ctx, tar(ctx, subj.clone(), b)?, tar(ctx, subj, c)?),
195+ None => {
196+ let foo = tar(ctx, subj, &b)?;
197+ let bar = tar(ctx, subj, &c)?;
198+ tar(ctx, &foo, &bar)
199+ }
207200 }
208201 }
209202 }
210203 }
211− _ => tar(ctx, tar(ctx, subj.clone(), b)?, tar(ctx, subj, c)?),
204+ _ => {
205+ let foo = tar(ctx, &subj, &b)?;
206+ let bar = tar(ctx, &subj, &c)?;
207+ tar(ctx, &foo, &bar)
208+ }
212209 }
213210 }
214− 3 => Some(wut(ctx, tar(ctx, subj, formula)?)),
215− 4 => Some(Rc::new(lus(tar(ctx, subj, formula)?)?)),
211+ 3 => Some(wut(ctx, tar(ctx, subj, &formula)?)),
212+ 4 => Some(Rc::new(lus(tar(ctx, subj, &formula)?)?)),
216213 5 => {
217214 let (b, c) = formula.as_cell()?;
218− Some(tis(ctx, tar(ctx, subj.clone(), b)?, tar(ctx, subj, c)?))
215+ let foo = tar(ctx, &subj, &b)?;
216+ Some(tis(ctx, foo, tar(ctx, subj, &c)?))
219217 }
220218 6 => {
221219 let (b, c) = formula.as_cell()?;
222220 let (c, d) = c.as_cell()?;
223221
224− tar(
222+ let foo = tar_u32(
225223 ctx,
226− subj.clone(),
224+ &cell(c, d),
225+ 0,
227226 tar_u32(
228227 ctx,
229− cell(c, d),
228+ &ctx.nouns.two_three,
230229 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))?,
236− )?,
230+ tar_u32(ctx, subj, 4, cell(ctx.nouns.four.clone(), b))?,
237231 )?,
238− )
232+ )?;
233+
234+ tar(ctx, &subj, &foo)
239235 }
240236 7 => {
241237 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)
243240 }
244241 8 => {
245242 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)
247244 }
248245 9 => {
249246 let (b, c) = formula.as_cell()?;
247+ let foo = tar(ctx, &subj, &c)?;
250248 tar_u32(
251249 ctx,
252− tar(ctx, subj, c)?,
250+ &foo,
253251 2,
254252 cell(ctx.nouns.sig_one.clone(), cell(ctx.nouns.sig.clone(), b)),
255253 )
@@ -259,21 +257,16 @@ fn tar_u32(
259257 let (b, c) = b.as_cell()?;
260258 let b = b.as_atom()?;
261259
262− hax(
263− ctx,
264− b.clone(),
265− tar(ctx, subj.clone(), c)?,
266− tar(ctx, subj, d)?,
267− )
260+ hax(ctx, b.clone(), tar(ctx, subj, &c)?, tar(ctx, subj, &d)?)
268261 }
269262 11 => {
270263 let (b, d) = formula.as_cell()?;
271264 // println!("{b}");
272265 match (*b).clone() {
273− Noun::Atom(_) => tar(ctx, subj, d),
266+ Noun::Atom(_) => tar(ctx, subj, &d),
274267 Noun::Cell { q: c, .. } => tar_u32(
275268 ctx,
276− cell(tar(ctx, subj.clone(), c)?, tar(ctx, subj, d)?),
269+ &cell(tar(ctx, subj, &c)?, tar(ctx, subj, &d)?),
277270 0,
278271 ctx.nouns.three.clone(),
279272 ),
@@ -283,10 +276,10 @@ fn tar_u32(
283276 }
284277}
285278
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>> {
287280 let (op, formula) = formula.as_cell()?;
288281 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)?)),
290283 Noun::Atom(ref op) => {
291284 let mut op_iter = op.iter_u32_digits();
292285 let op = match op_iter.len() {
@@ -295,7 +288,7 @@ pub fn tar(ctx: &InterpreterContext, subj: Rc<Noun>, formula: Rc<Noun>) -> Optio
295288 _ => None,
296289 };
297290 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))
299292 }
300293 }
301294}
@@ -303,8 +296,8 @@ pub fn tar(ctx: &InterpreterContext, subj: Rc<Noun>, formula: Rc<Noun>) -> Optio
303296pub fn eval_gate(ctx: &InterpreterContext, gate: Rc<Noun>) -> Option<Rc<Noun>> {
304297 tar(
305298 ctx,
306− gate,
307− cell(
299+ &gate,
300+ &cell(
308301 Rc::new(Noun::Atom(BigUint::new(vec![9]))),
309302 cell(
310303 ctx.nouns.two.clone(),
nock/src/jets.rs+14−1
@@ -27,7 +27,10 @@ pub type Jets = BTreeMap<noun::Hash, Jet>;
2727pub type DoubleJets = BTreeMap<noun::Hash, DoubleJet>;
2828
2929pub fn generate_double_jets() -> DoubleJets {
30− BTreeMap::from([(210326504639957723220404138306543145797, JUST)])
30+ BTreeMap::from([
31+ (210326504639957723220404138306543145797, JUST),
32+ // (255213039400865122956425872354360738044, STIR),
33+ ])
3134}
3235
3336pub fn generate_jets() -> Jets {
@@ -960,6 +963,16 @@ static JUST: DoubleJet = |ctx, n, m| {
960963 }
961964};
962965
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+
963976fn next(nail: Nail) -> Option<Edge> {
964977 if *nail.rest == Noun::SIG {
965978 Some(fail(nail))
nock/src/main.rs+28−16
@@ -4,7 +4,7 @@ use nock::{
44 cue::cue_bytes,
55 interpreter::{self, eval_gate, generate_interpreter_context, slam},
66 jam::jam_to_bytes,
7− noun::{Atom, Noun},
7+ noun::{cell, Atom, Noun},
88};
99use spinoff::Spinner;
1010use std::{
@@ -149,28 +149,40 @@ fn new_spinner(txt: String) -> Spinner {
149149}
150150
151151fn build(root: PathBuf, output: PathBuf) -> Result<(), std::io::Error> {
152− let make_nock = include_bytes!("../res/make.nock");
153− let make = cue_bytes(make_nock);
152+ println!("reaming");
153+ let root_ast = {
154+ let ream_nock = include_bytes!("../res/ream.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+ };
154166
155− let root = read_file(&root)?;
167+ println!("Loading hoon/hoon");
156168
157− let hoon_hoon = include_bytes!("../res/hoon.hoon");
169+ let ast = {
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+ };
158177
159− let mut source = Vec::new();
178+ println!("minting");
160179
161− source.extend_from_slice(b"=>\n");
162− source.extend_from_slice(hoon_hoon);
163− source.extend_from_slice(&root);
180+ let mint_gate = cue_bytes(include_bytes!("../res/mint.nock"));
181+ let slam_result = slam(&generate_interpreter_context(), mint_gate, ast).unwrap();
164182
165− let slam_result = slam(
166− &generate_interpreter_context(),
167− make,
168− Rc::new(Noun::Atom(Atom::from_bytes_le(&source))),
169− )
170− .unwrap();
183+ // println!("{slam_result}");
171184
172185 let mut output_file = File::create(output)?;
173186 File::write_all(&mut output_file, &jam_to_bytes(slam_result)).unwrap();
174187 Ok(())
175188}
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 {
400400}
401401
402402pub fn cell(p: Rc<Noun>, q: Rc<Noun>) -> Rc<Noun> {
403− Rc::new(Noun::Cell {
403+ Rc::new(naked_cell(p, q))
404+}
405+
406+pub fn naked_cell(p: Rc<Noun>, q: Rc<Noun>) -> Noun {
407+ Noun::Cell {
404408 p: p.clone(),
405409 q: q.clone(),
406410 hash: Cell::new(None),
407411 compiled_gate: None,
408− })
412+ }
409413}
410414
411415fn hash_pair(p: Rc<Noun>, q: Rc<Noun>) -> Hash {