bf

a simple toy bf compiler in hare, made for teaching!
Log | Files | Refs

parse.ha (662B)


      1 use io;
      2 use fmt;
      3 use bufio;
      4 
      5 export fn load(h: io::handle, inblock: bool = false) (*block | io::EOF | io::error) = {
      6 	let insts: [](u8 | *block) = [];
      7 
      8 	for (true) {
      9 		let c = match (bufio::read_byte(h)?) {
     10 		case let c: u8 => yield c;
     11 		case io::EOF =>
     12 			if (inblock) fmt::fatal("unexpected EOF");
     13 			return alloc(insts)!: *block;
     14 		};
     15 
     16 		switch (c) {
     17 		case '+', '-', '>', '<', '.', ',' => append(insts, c)!;
     18 		case '[' =>
     19 			let nested = load(h, true)! as *block;
     20 			append(insts, nested)!;
     21 		case ']' => return alloc(insts)!: *block;
     22 		case => fmt::fatalf("unexpected token type {}", c: rune);
     23 		};
     24 	};
     25 };
     26 
     27 export fn finish(b: *block) void = {
     28 	free(b);
     29 };