os

An operating system
git clone https://erai.gay/code/os/
Log | Files | Refs | README | LICENSE

commit 79f1b8bdb324674a38da381255a159793775a0e1
parent f2cc7778e6a8922ae886ac1df50c01e87d17e3a5
Author: erai <erai@omiltem.net>
Date:   Sat,  1 Feb 2025 16:23:36 +0000

remove unused functions

Diffstat:
Mattic/genlex.om | 156++++++++++++++++++++++++++++++++++++++++----------------------------------------
Mbufio.om | 7++++++-
Mcc1.om | 47++++++++++++++++++++---------------------------
Mcmp.om | 29++++++++++++++++-------------
Mcout.om | 1-
Mcpio.om | 20+++++++++++---------
Mecho.om | 7++++---
Dgenlex.om | 1115-------------------------------------------------------------------------------
Mkernel.om | 9---------
Mlib.om | 171+++----------------------------------------------------------------------------
Mls.om | 9++++++---
Mparse2.om | 35+++++------------------------------
Mpeg.om | 5++---
Mpeglib.om | 26+++++++++++++-------------
Msh.om | 12+++++++++++-
Mtype.om | 16----------------
Aunused.sh | 3+++
Mxxd.om | 11+++++++----
18 files changed, 187 insertions(+), 1492 deletions(-)

diff --git a/attic/genlex.om b/attic/genlex.om @@ -1,4 +1,4 @@ -getchar(): int { +func getchar(): int { var b: byte; var ret: int; ret = read(0, &b, 1); @@ -8,13 +8,13 @@ getchar(): int { if (ret == 0) { return -1; } - return b: int; + return b as int; } -putchar(ch: int): void { +func putchar(ch: int): void { var b: byte; var ret: int; - b = ch: byte; + b = ch as byte; ret = write(1, &b, 1); if (ret != 1) { exit(3); @@ -38,25 +38,25 @@ struct compiler { d: *dfa; } -setup(c: *compiler): void { +func setup(c: *compiler): void { setup_alloc(&c.a); c.nc = getchar(); c.lineno = 1; c.colno = 1; c.tt = 0; - c.n = 0: *nfa; + c.n = nil; c.tmax = 256; c.tlen = 0; c.buf = alloc(&c.a, c.tmax); - c.tags = 0: *tag; + c.tags = nil; c.ntags = 0; c.nnfa = 0; c.ndfa = 0; - c.d = 0:*dfa; + c.d = nil; feed(c); } -feedc(c: *compiler): void { +func feedc(c: *compiler): void { c.nc = getchar(); if (c.nc == '\n') { c.lineno = c.lineno + 1; @@ -81,8 +81,8 @@ enum { T_SEMI, } -feed(c: *compiler): void { - c.n = 0:*nfa; +func feed(c: *compiler): void { + c.n = nil; c.tlen = 0; loop { @@ -146,7 +146,7 @@ feed(c: *compiler): void { } } -feed_ident(c: *compiler): void { +func feed_ident(c: *compiler): void { c.tt = T_IDENT; loop { if (!((c.nc >= 'a' && c.nc <= 'z') || @@ -156,18 +156,18 @@ feed_ident(c: *compiler): void { break; } - c.buf[c.tlen] = c.nc:byte; + c.buf[c.tlen] = c.nc as byte; c.tlen = c.tlen + 1; if (c.tlen == c.tmax) { die("ident too long"); } - c.buf[c.tlen] = 0:byte; + c.buf[c.tlen] = 0 as byte; feedc(c); } } -hexdig(c: *compiler): int { +func hexdig(c: *compiler): int { if (c.nc >= '0' && c.nc <= '9') { return c.nc - '0'; } @@ -183,7 +183,7 @@ hexdig(c: *compiler): int { die("invalid hex digit"); } -feed_escape(c: *compiler): void { +func feed_escape(c: *compiler): void { var hex: int; feedc(c); @@ -217,7 +217,7 @@ feed_escape(c: *compiler): void { } } -feed_literal(c: *compiler): void { +func feed_literal(c: *compiler): void { var a: *nfa; c.tt = T_LITERAL; @@ -239,14 +239,14 @@ feed_literal(c: *compiler): void { feed_escape(c); } - a = nfa_literal(c, c.nc: byte); + a = nfa_literal(c, c.nc as byte); c.n = nfa_concat(c, c.n, a); feedc(c); } } -feed_charset(c: *compiler): void { +func feed_charset(c: *compiler): void { var left: int; var right: int; var mode: int; @@ -266,7 +266,7 @@ feed_charset(c: *compiler): void { if (i == 256) { break; } - c.buf[i] = 0: byte; + c.buf[i] = 0 as byte; i = i + 1; } @@ -292,7 +292,7 @@ feed_charset(c: *compiler): void { if (i == 256) { break; } - c.buf[i] = 1:byte; + c.buf[i] = 1 as byte; i = i + 1; } } @@ -334,7 +334,7 @@ feed_charset(c: *compiler): void { if (i >= right) { break; } - c.buf[i] = mode: byte; + c.buf[i] = mode as byte; i = i + 1; } } @@ -385,10 +385,10 @@ feed_charset(c: *compiler): void { } } -parse_ident(c: *compiler): *tag { +func parse_ident(c: *compiler): *tag { var t: *tag; if (c.tt != T_IDENT) { - return 0: *tag; + return nil; } t = find_tag(c, c.buf); feed(c); @@ -401,7 +401,7 @@ struct tag { id: int; } -intern(c: *compiler): *byte { +func intern(c: *compiler): *byte { var s: *byte; var i: int; s = alloc(&c.a, c.tlen + 1); @@ -413,11 +413,11 @@ intern(c: *compiler): *byte { s[i] = c.buf[i]; i = i + 1; } - s[c.tlen] = 0:byte; + s[c.tlen] = 0 as byte; return s; } -find_tag(c: *compiler, s: *byte): *tag { +func find_tag(c: *compiler, s: *byte): *tag { var t: *tag; var link: **tag; link = &c.tags; @@ -431,8 +431,8 @@ find_tag(c: *compiler, s: *byte): *tag { } link = &t.next; } - t = alloc(&c.a, sizeof(*t)): *tag; - t.next = 0:*tag; + t = alloc(&c.a, sizeof(*t)) as *tag; + t.next = nil; t.s = intern(c); t.id = c.ntags; c.ntags = c.ntags + 1; @@ -451,29 +451,29 @@ struct nfa { end: *nfa; } -nfa_empty(c: *compiler): *nfa { +func nfa_empty(c: *compiler): *nfa { var n: *nfa; - n = alloc(&c.a, sizeof(*n)):*nfa; + n = alloc(&c.a, sizeof(*n)) as *nfa; n.id = c.nnfa; n.left = -1; n.right = -1; n.live = 0; - n.a = 0:*nfa; - n.b = 0:*nfa; + n.a = nil; + n.b = nil; n.end = n; c.nnfa = c.nnfa + 1; return n; } -nfa_literal(c: *compiler, a: byte): *nfa { +func nfa_literal(c: *compiler, a: byte): *nfa { var n: *nfa; n = nfa_empty(c); - n.left = a:int; + n.left = a as int; n.right = n.left + 1; return n; } -nfa_alt(c: *compiler, a: *nfa, b: *nfa): *nfa { +func nfa_alt(c: *compiler, a: *nfa, b: *nfa): *nfa { var i: *nfa; var o: *nfa; i = nfa_empty(c); @@ -486,13 +486,13 @@ nfa_alt(c: *compiler, a: *nfa, b: *nfa): *nfa { return i; } -nfa_concat(c: *compiler, a: *nfa, b: *nfa): *nfa { +func nfa_concat(c: *compiler, a: *nfa, b: *nfa): *nfa { a.end.a = b; a.end = b.end; return a; } -nfa_plus(c: *compiler, a: *nfa): *nfa { +func nfa_plus(c: *compiler, a: *nfa): *nfa { var o: *nfa; o = nfa_empty(c); o.b = a; @@ -501,43 +501,43 @@ nfa_plus(c: *compiler, a: *nfa): *nfa { return a; } -nfa_qmark(c: *compiler, a: *nfa): *nfa { +func nfa_qmark(c: *compiler, a: *nfa): *nfa { var b: *nfa; b = nfa_empty(c); a = nfa_alt(c, a, b); return a; } -nfa_star(c: *compiler, a: *nfa): *nfa { +func nfa_star(c: *compiler, a: *nfa): *nfa { a = nfa_plus(c, a); a = nfa_qmark(c, a); return a; } -parse_literal(c: *compiler): *nfa { +func parse_literal(c: *compiler): *nfa { var n: *nfa; if (c.tt != T_LITERAL) { - return 0:*nfa; + return nil; } n = c.n; feed(c); return n; } -parse_charset(c: *compiler): *nfa { +func parse_charset(c: *compiler): *nfa { var n: *nfa; if (c.tt != T_CHARSET) { - return 0:*nfa; + return nil; } n = c.n; feed(c); return n; } -parse_dot(c: *compiler): *nfa { +func parse_dot(c: *compiler): *nfa { var n: *nfa; if (c.tt != T_DOT) { - return 0:*nfa; + return nil; } feed(c); n = nfa_empty(c); @@ -550,7 +550,7 @@ parse_dot(c: *compiler): *nfa { // | charset // | dot // | '(' regex ')' -parse_primary(c: *compiler): *nfa { +func parse_primary(c: *compiler): *nfa { var n: *nfa; n = parse_literal(c); @@ -581,19 +581,19 @@ parse_primary(c: *compiler): *nfa { return n; } - return 0: *nfa; + return nil; } // post := primary // | post '*' // | post '+' // | post '?' -parse_post(c: *compiler): *nfa { +func parse_post(c: *compiler): *nfa { var n: *nfa; n = parse_primary(c); if (!n) { - return 0: *nfa; + return nil; } loop { @@ -614,13 +614,13 @@ parse_post(c: *compiler): *nfa { // concat := post // | post concat -parse_concat(c: *compiler): *nfa { +func parse_concat(c: *compiler): *nfa { var n: *nfa; var b: *nfa; n = parse_post(c); if (!n) { - return 0:*nfa; + return nil; } loop { @@ -636,7 +636,7 @@ parse_concat(c: *compiler): *nfa { // regex := concat // | '|' regex // | concat '|' regex -parse_regex(c: *compiler): *nfa { +func parse_regex(c: *compiler): *nfa { var n: *nfa; var b: *nfa; @@ -661,14 +661,14 @@ parse_regex(c: *compiler): *nfa { } // decl := ident '=' regex ';' -parse_decl(c: *compiler): *nfa { +func parse_decl(c: *compiler): *nfa { var regex: *nfa; var t: *tag; var n: *nfa; t = parse_ident(c); if (!t) { - return 0: *nfa; + return nil; } if (c.tt != T_EQUALS) { @@ -691,11 +691,11 @@ parse_decl(c: *compiler): *nfa { // progam := decl // | decl program -parse_program(c: *compiler): *nfa { +func parse_program(c: *compiler): *nfa { var n: *nfa; var p: *nfa; - p = 0: *nfa; + p = nil; loop { n = parse_decl(c); if (!n) { @@ -729,14 +729,14 @@ struct nlist { tag: *tag; } -alloc_nlist(c: *compiler, l: *nlist, cap: int): void { +func alloc_nlist(c: *compiler, l: *nlist, cap: int): void { l.cap = cap; - l.live = alloc(&c.a, sizeof(*l.live) * cap):**nfa; + l.live = alloc(&c.a, sizeof(*l.live) * cap) as **nfa; l.fill = 0; - l.tag = 0:*tag; + l.tag = nil; } -activate(l: *nlist, n: *nfa): void { +func activate(l: *nlist, n: *nfa): void { if (n.live) { return; } @@ -761,7 +761,7 @@ activate(l: *nlist, n: *nfa): void { } } -nlist_cmp(a: *nlist, b: *nlist): int { +func nlist_cmp(a: *nlist, b: *nlist): int { var i: int; i = 0; @@ -812,7 +812,7 @@ nlist_cmp(a: *nlist, b: *nlist): int { return 0; } -nlist_sort(l: *nlist): void { +func nlist_sort(l: *nlist): void { var i: int; var j: int; var k: int; @@ -873,22 +873,22 @@ nlist_sort(l: *nlist): void { } } -alloc_link(c: *compiler): **dfa { +func alloc_link(c: *compiler): **dfa { var link: **dfa; var i: int; - link = alloc(&c.a, sizeof(*link) * 256): **dfa; + link = alloc(&c.a, sizeof(*link) * 256) as **dfa; i = 0; loop { if (i == 256) { break; } - link[i] = 0:*dfa; + link[i] = nil; i = i + 1; } return link; } -nlist_copy(c: *compiler, dest: *nlist, src: *nlist): void { +func nlist_copy(c: *compiler, dest: *nlist, src: *nlist): void { var i: int; alloc_nlist(c, dest, src.fill); dest.fill = src.fill; @@ -903,7 +903,7 @@ nlist_copy(c: *compiler, dest: *nlist, src: *nlist): void { } } -nlist2dfa(c: *compiler, l: *nlist): *dfa { +func nlist2dfa(c: *compiler, l: *nlist): *dfa { var link: **dfa; var d: *dfa; var n: *nfa; @@ -913,7 +913,7 @@ nlist2dfa(c: *compiler, l: *nlist): *dfa { var j: int; if (l.fill == 0 && !l.tag) { - return 0:*dfa; + return nil; } link = &c.d; @@ -934,12 +934,12 @@ nlist2dfa(c: *compiler, l: *nlist): *dfa { } } - d = alloc(&c.a, sizeof(*d)): *dfa; + d = alloc(&c.a, sizeof(*d)) as *dfa; d.id = c.ndfa; d.link = alloc_link(c); nlist_copy(c, &d.key, l); - d.l = 0: *dfa; - d.r = 0: *dfa; + d.l = nil; + d.r = nil; d.seen = 0; c.ndfa = c.ndfa + 1; @@ -980,13 +980,13 @@ nlist2dfa(c: *compiler, l: *nlist): *dfa { return d; } -deactivate(l: *nlist): void { +func deactivate(l: *nlist): void { var i: int; i = 0; loop { if (i >= l.fill) { l.fill = 0; - l.tag = 0: *tag; + l.tag = nil; break; } l.live[i].live = 0; @@ -994,14 +994,14 @@ deactivate(l: *nlist): void { } } -powerset(c: *compiler, n: *nfa): *dfa { +func powerset(c: *compiler, n: *nfa): *dfa { var live: nlist; alloc_nlist(c, &live, c.nnfa); activate(&live, n); return nlist2dfa(c, &live); } -codegen(c: *compiler, a: *dfa): void { +func codegen(c: *compiler, a: *dfa): void { var i: int; var b: *dfa; var lo: int; @@ -1080,7 +1080,7 @@ codegen(c: *compiler, a: *dfa): void { } } -gen(c: *compiler, a: *dfa): void { +func gen(c: *compiler, a: *dfa): void { var t: *tag; t = c.tags; fdputs(1, "enum {\n"); @@ -1104,7 +1104,7 @@ gen(c: *compiler, a: *dfa): void { fdputs(1, "}\n"); } -main(argc: int, argv: **byte, envp: **byte): void { +func main(argc: int, argv: **byte, envp: **byte): void { var c: compiler; var n: *nfa; var a: *dfa; diff --git a/bufio.om b/bufio.om @@ -87,8 +87,13 @@ func ffill(f: *file): void { } func fputc(f: *file, ch: int): void { + var b: byte; + if !f { - fdputc(1, ch); + b = ch as byte; + if write(1, &b, 1) != 1 { + exit(3); + } return; } diff --git a/cc1.om b/cc1.om @@ -2,6 +2,8 @@ struct compiler { // Allocator a: *alloc; + err: *file; + // Parser p: *parser; @@ -32,32 +34,34 @@ struct compiler { } func cshow_context(c: *compiler) { - fdputs(2, "on "); + fputs(c.err, "on "); if (c.filename) { - fdputs(2, c.filename); + fputs(c.err, c.filename); } - fdputs(2, ":"); - fdputd(2, c.lineno); - fdputs(2, ":"); - fdputd(2, c.colno); - fdputs(2, "\n"); + fputs(c.err, ":"); + fputd(c.err, c.lineno); + fputs(c.err, ":"); + fputd(c.err, c.colno); + fputs(c.err, "\n"); } func cdie(c: *compiler, msg: *byte) { cshow_context(c); - fdputs(2, "cdie: "); - fdputs(2, msg); - fdputs(2, "\n"); + fputs(c.err, "cdie: "); + fputs(c.err, msg); + fputs(c.err, "\n"); exit(1); } -func comp_setup(a: *alloc): *compiler { +func comp_setup(a: *alloc, err: *file): *compiler { var c: *compiler; c = alloc(a, sizeof(*c)) as *compiler; c.a = a; + c.err = err; + c.p = setup_parser(a); c.filename = nil; @@ -1388,24 +1392,12 @@ func call_check(c: *compiler, n: *node): int { || n.kind == N_IDENT || n.kind == N_SIZEOF || n.kind == N_NIL { // No side effects } else { - fdputd(2, n.kind); die("invalid expr"); } return result; } -func update_place(c: *compiler, n: *node) { - if !n { - return; - } - c.filename = n.filename; - c.lineno = n.lineno; - c.colno = n.colno; - c.s.filename = n.filename; - c.s.lineno = n.lineno; -} - func emit_ssr(c: *compiler) { var d: *decl; var v: *decl; @@ -2211,7 +2203,9 @@ func main(argc: int, argv: **byte, envp: **byte) { setup_alloc(&a); - c = comp_setup(&a); + err = fopen(2, &a); + + c = comp_setup(&a, err); show = 0; @@ -2298,7 +2292,7 @@ func main(argc: int, argv: **byte, envp: **byte) { if !tmp { break; } - peg_compile(peg, tmp.name); + peg_compile(peg, tmp.name, err); tmp = tmp.next; } return; @@ -2309,12 +2303,11 @@ func main(argc: int, argv: **byte, envp: **byte) { if !tmp { break; } - p = concat_program(p, parse(c.p, tmp.name)); + p = concat_program(p, parse(c.p, tmp.name, err)); tmp = tmp.next; } if show { - err = fopen(2, &a); show_node(err, p); fflush(err); return; diff --git a/cmp.om b/cmp.om @@ -4,28 +4,31 @@ func main(argc: int, argv: **byte, envp: **byte) { var alloc: alloc; var fa: *file; var fb: *file; + var out: *file; var c: int; var i: int; setup_alloc(&alloc); + out = fopen(1, &alloc); + if argc != 3 { die("usage: cmp file1 file2"); } a = open(argv[1], 0, 0); if a < 0 { - fdputs(1, "failed to open: "); - fdputs(1, argv[1]); - fdputs(1, "\n"); + fputs(out, "failed to open: "); + fputs(out, argv[1]); + fputs(out, "\n"); exit(2); } b = open(argv[2], 0, 0); if b < 0 { - fdputs(1, "failed to open: "); - fdputs(1, argv[2]); - fdputs(1, "\n"); + fputs(out, "failed to open: "); + fputs(out, argv[2]); + fputs(out, "\n"); exit(2); } @@ -37,13 +40,13 @@ func main(argc: int, argv: **byte, envp: **byte) { c = fgetc(fa); if c != fgetc(fb) { - fdputs(1, argv[1]); - fdputc(1, ' '); - fdputs(1, argv[2]); - fdputc(1, ' '); - fdputs(1, "differ: byte "); - fdputd(1, i); - fdputc(1, '\n'); + fputs(out, argv[1]); + fputc(out, ' '); + fputs(out, argv[2]); + fputc(out, ' '); + fputs(out, "differ: byte "); + fputd(out, i); + fputc(out, '\n'); exit(1); } diff --git a/cout.om b/cout.om @@ -532,7 +532,6 @@ func ctranslate_expr(c: *compiler, n: *node) { fputs(c.cout, ")"); ctranslate_expr(c, n.a); } else { - fdputd(2, n.kind); die("invalid expr"); } } diff --git a/cpio.om b/cpio.om @@ -27,6 +27,7 @@ func main(argc: int, argv: **byte, envp: **byte) { var name: *byte; var stdin: *file; var stdout: *file; + var stderr: *file; var stat: stat; var buf: *byte; var len: int; @@ -54,6 +55,7 @@ func main(argc: int, argv: **byte, envp: **byte) { stdin = fopen(0, &a); stdout = fopen(1, &a); + stderr = fopen(2, &a); name = alloc(&a, 4096); buf = alloc(&a, 4096); @@ -70,26 +72,26 @@ func main(argc: int, argv: **byte, envp: **byte) { if len == 4096 { fflush(stdout); - fdputs(2, "name truncated: "); - fdputs(2, name); - fdputs(2, "\n"); + fputs(stderr, "name truncated: "); + fputs(stderr, name); + fputs(stderr, "\n"); exit(1); } fd = open(name, 0, 0); if fd < 0 { fflush(stdout); - fdputs(2, "failed to open: "); - fdputs(2, name); - fdputs(2, "\n"); + fputs(stderr, "failed to open: "); + fputs(stderr, name); + fputs(stderr, "\n"); exit(1); } if fstat(fd, (&stat) as *byte) < 0 { fflush(stdout); - fdputs(2, "stat failed: "); - fdputs(2, name); - fdputs(2, "\n"); + fputs(stderr, "stat failed: "); + fputs(stderr, name); + fputs(stderr, "\n"); exit(1); } diff --git a/echo.om b/echo.om @@ -1,18 +1,19 @@ func main(argc: int, argv: **byte, envp: **byte) { var i: int; + i = 1; loop { if i >= argc { - fdputc(1, '\n'); + write(1, "\n", 1); return; } - fdputs(1, argv[i]); + write(1, argv[i], strlen(argv[i])); i = i + 1; if i < argc { - fdputc(1, ' '); + write(1, " ", 1); } } } diff --git a/genlex.om b/genlex.om @@ -1,1115 +0,0 @@ -func getchar(): int { - var b: byte; - var ret: int; - ret = read(0, &b, 1); - if (ret < 0) { - exit(3); - } - if (ret == 0) { - return -1; - } - return b as int; -} - -func putchar(ch: int): void { - var b: byte; - var ret: int; - b = ch as byte; - ret = write(1, &b, 1); - if (ret != 1) { - exit(3); - } -} - -struct compiler { - a: alloc; - nc: int; - lineno: int; - colno: int; - tt: int; - n: *nfa; - buf: *byte; - tlen: int; - tmax: int; - tags: *tag; - ntags: int; - nnfa: int; - ndfa: int; - d: *dfa; -} - -func setup(c: *compiler): void { - setup_alloc(&c.a); - c.nc = getchar(); - c.lineno = 1; - c.colno = 1; - c.tt = 0; - c.n = nil; - c.tmax = 256; - c.tlen = 0; - c.buf = alloc(&c.a, c.tmax); - c.tags = nil; - c.ntags = 0; - c.nnfa = 0; - c.ndfa = 0; - c.d = nil; - feed(c); -} - -func feedc(c: *compiler): void { - c.nc = getchar(); - if (c.nc == '\n') { - c.lineno = c.lineno + 1; - c.colno = 0; - } - c.colno = c.colno +1; -} - -enum { - T_EOF, - T_IDENT, - T_LITERAL, - T_CHARSET, - T_DOT, - T_STAR, - T_PLUS, - T_QMARK, - T_LPAR, - T_RPAR, - T_ALT, - T_EQUALS, - T_SEMI, -} - -func feed(c: *compiler): void { - c.n = nil; - c.tlen = 0; - - loop { - if (c.nc == -1) { - c.tt = T_EOF; - return; - } else if (c.nc == ' ' || c.nc == '\t' || c.nc == '\r' || c.nc == '\n') { - feedc(c); - } else if (c.nc == '/') { - feedc(c); - if (c.nc == '/') { - loop { - if (c.nc == '\n' || c.nc == -1) { - break; - } - feedc(c); - } - } else { - die("slash but a comment"); - } - } else { - break; - } - } - - if ((c.nc >= 'a' && c.nc <= 'z') || (c.nc >= 'A' && c.nc <= 'Z') || c.nc == '_') { - feed_ident(c); - } else if (c.nc == '"') { - feed_literal(c); - } else if (c.nc == '[') { - feed_charset(c); - } else if (c.nc == '.') { - c.tt = T_DOT; - feedc(c); - } else if (c.nc == '*') { - c.tt = T_STAR; - feedc(c); - } else if (c.nc == '+') { - c.tt = T_PLUS; - feedc(c); - } else if (c.nc == '?') { - c.tt = T_QMARK; - feedc(c); - } else if (c.nc == '(') { - c.tt = T_LPAR; - feedc(c); - } else if (c.nc == ')') { - c.tt = T_RPAR; - feedc(c); - } else if (c.nc == '|') { - c.tt = T_ALT; - feedc(c); - } else if (c.nc == '=') { - c.tt = T_EQUALS; - feedc(c); - } else if (c.nc == ';') { - c.tt = T_SEMI; - feedc(c); - } else { - die("invalid char"); - } -} - -func feed_ident(c: *compiler): void { - c.tt = T_IDENT; - loop { - if (!((c.nc >= 'a' && c.nc <= 'z') || - (c.nc >= 'A' && c.nc <= 'Z') || - (c.nc >= '0' && c.nc <= '9') || - c.nc == '_')) { - break; - } - - c.buf[c.tlen] = c.nc as byte; - c.tlen = c.tlen + 1; - if (c.tlen == c.tmax) { - die("ident too long"); - } - c.buf[c.tlen] = 0 as byte; - - feedc(c); - } -} - -func hexdig(c: *compiler): int { - if (c.nc >= '0' && c.nc <= '9') { - return c.nc - '0'; - } - - if (c.nc >= 'A' && c.nc <= 'F') { - return (c.nc - 'A') + 10; - } - - if (c.nc >= 'a' && c.nc <= 'f') { - return (c.nc - 'a') + 10; - } - - die("invalid hex digit"); -} - -func feed_escape(c: *compiler): void { - var hex: int; - - feedc(c); - - if (c.nc == 't') { - c.nc = '\t'; - } else if (c.nc == 'r') { - c.nc = '\r'; - } else if (c.nc == 'n') { - c.nc = '\n'; - } else if (c.nc == '\\') { - c.nc = '\\'; - } else if (c.nc == '\'') { - c.nc = '\''; - } else if (c.nc == '\"') { - c.nc = '\"'; - } else if (c.nc == '-') { - c.nc = '-'; - } else if (c.nc == '[') { - c.nc = '['; - } else if (c.nc == ']') { - c.nc = ']'; - } else if (c.nc == 'x') { - feedc(c); - hex = hexdig(c) * 16; - feedc(c); - hex = hex + hexdig(c); - c.nc = hex; - } else { - die("invalid escape"); - } -} - -func feed_literal(c: *compiler): void { - var a: *nfa; - - c.tt = T_LITERAL; - c.n = nfa_empty(c); - - feedc(c); - - loop { - if (c.nc == '"') { - feedc(c); - return; - } - - if (c.nc == -1 || c.nc == 0 || c.nc == '\n') { - die("invalid char in literal"); - } - - if (c.nc == '\\') { - feed_escape(c); - } - - a = nfa_literal(c, c.nc as byte); - c.n = nfa_concat(c, c.n, a); - - feedc(c); - } -} - -func feed_charset(c: *compiler): void { - var left: int; - var right: int; - var mode: int; - var nonempty: int; - var i: int; - var a: *nfa; - - c.tt = T_CHARSET; - - feedc(c); - - mode = 1; - nonempty = 0; - - i = left; - loop { - if (i == 256) { - break; - } - c.buf[i] = 0 as byte; - i = i + 1; - } - - loop { - if (c.nc == ']') { - feedc(c); - break; - } - - if (c.nc == -1 || c.nc == 0 || c.nc == '\n' || c.nc == '-') { - die("invalid char in charset"); - } - - if (c.nc == '^') { - if (!mode) { - die("invalid char in charset"); - } - - feedc(c); - if (mode && !nonempty) { - i = 0; - loop { - if (i == 256) { - break; - } - c.buf[i] = 1 as byte; - i = i + 1; - } - } - mode = 0; - continue; - } - - if (c.nc == '\\') { - feed_escape(c); - left = c.nc; - feedc(c); - } else { - left = c.nc; - feedc(c); - } - - if (c.nc == '-') { - feedc(c); - - if (c.nc == -1 || c.nc == 0 || c.nc == '\n' || c.nc == '-') { - die("invalid char in charset"); - } - - if (c.nc == '\\') { - feed_escape(c); - right = c.nc + 1; - feedc(c); - } else { - right = c.nc + 1; - feedc(c); - } - } else { - right = left + 1; - } - - nonempty = 1; - i = left; - loop { - if (i >= right) { - break; - } - c.buf[i] = mode as byte; - i = i + 1; - } - } - - c.n = nfa_empty(c); - c.n.left = 256; - c.n.right = 256; - - i = 0; - loop { - loop { - if (i == 256) { - left = 256; - break; - } - - if (c.buf[i]) { - left = i; - break; - } - - i = i + 1; - } - - loop { - if (i == 256) { - right = 256; - break; - } - - if (!c.buf[i]) { - right = i; - break; - } - - i = i + 1; - } - - if (left >= right) { - break; - } - - a = nfa_empty(c); - a.left = left; - a.right = right; - - c.n = nfa_alt(c, c.n, a); - } -} - -func parse_ident(c: *compiler): *tag { - var t: *tag; - if (c.tt != T_IDENT) { - return nil; - } - t = find_tag(c, c.buf); - feed(c); - return t; -} - -struct tag { - next: *tag; - s: *byte; - id: int; -} - -func intern(c: *compiler): *byte { - var s: *byte; - var i: int; - s = alloc(&c.a, c.tlen + 1); - i = 0; - loop { - if (i == c.tlen) { - break; - } - s[i] = c.buf[i]; - i = i + 1; - } - s[c.tlen] = 0 as byte; - return s; -} - -func find_tag(c: *compiler, s: *byte): *tag { - var t: *tag; - var link: **tag; - link = &c.tags; - loop { - t = *link; - if (!t) { - break; - } - if (!strcmp(t.s, s)) { - return t; - } - link = &t.next; - } - t = alloc(&c.a, sizeof(*t)) as *tag; - t.next = nil; - t.s = intern(c); - t.id = c.ntags; - c.ntags = c.ntags + 1; - *link = t; - return t; -} - -struct nfa { - id: int; - tag: *tag; - left: int; - right: int; - live: int; - a: *nfa; - b: *nfa; - end: *nfa; -} - -func nfa_empty(c: *compiler): *nfa { - var n: *nfa; - n = alloc(&c.a, sizeof(*n)) as *nfa; - n.id = c.nnfa; - n.left = -1; - n.right = -1; - n.live = 0; - n.a = nil; - n.b = nil; - n.end = n; - c.nnfa = c.nnfa + 1; - return n; -} - -func nfa_literal(c: *compiler, a: byte): *nfa { - var n: *nfa; - n = nfa_empty(c); - n.left = a as int; - n.right = n.left + 1; - return n; -} - -func nfa_alt(c: *compiler, a: *nfa, b: *nfa): *nfa { - var i: *nfa; - var o: *nfa; - i = nfa_empty(c); - o = nfa_empty(c); - i.a = a; - i.b = b; - i.end = o; - a.end.a = o; - b.end.a = o; - return i; -} - -func nfa_concat(c: *compiler, a: *nfa, b: *nfa): *nfa { - a.end.a = b; - a.end = b.end; - return a; -} - -func nfa_plus(c: *compiler, a: *nfa): *nfa { - var o: *nfa; - o = nfa_empty(c); - o.b = a; - a.end.a = o; - a.end = o; - return a; -} - -func nfa_qmark(c: *compiler, a: *nfa): *nfa { - var b: *nfa; - b = nfa_empty(c); - a = nfa_alt(c, a, b); - return a; -} - -func nfa_star(c: *compiler, a: *nfa): *nfa { - a = nfa_plus(c, a); - a = nfa_qmark(c, a); - return a; -} - -func parse_literal(c: *compiler): *nfa { - var n: *nfa; - if (c.tt != T_LITERAL) { - return nil; - } - n = c.n; - feed(c); - return n; -} - -func parse_charset(c: *compiler): *nfa { - var n: *nfa; - if (c.tt != T_CHARSET) { - return nil; - } - n = c.n; - feed(c); - return n; -} - -func parse_dot(c: *compiler): *nfa { - var n: *nfa; - if (c.tt != T_DOT) { - return nil; - } - feed(c); - n = nfa_empty(c); - n.left = 0; - n.right = 256; - return n; -} - -// primary := literal -// | charset -// | dot -// | '(' regex ')' -func parse_primary(c: *compiler): *nfa { - var n: *nfa; - - n = parse_literal(c); - if (n) { - return n; - } - - n = parse_charset(c); - if (n) { - return n; - } - - n = parse_dot(c); - if (n) { - return n; - } - - if (c.tt == T_LPAR) { - feed(c); - - n = parse_regex(c); - - if (c.tt != T_RPAR) { - die("expected )"); - } - feed(c); - - return n; - } - - return nil; -} - -// post := primary -// | post '*' -// | post '+' -// | post '?' -func parse_post(c: *compiler): *nfa { - var n: *nfa; - - n = parse_primary(c); - if (!n) { - return nil; - } - - loop { - if (c.tt == T_STAR) { - n = nfa_star(c, n); - feed(c); - } else if (c.tt == T_PLUS) { - n = nfa_plus(c, n); - feed(c); - } else if (c.tt == T_QMARK) { - n = nfa_qmark(c, n); - feed(c); - } else { - return n; - } - } -} - -// concat := post -// | post concat -func parse_concat(c: *compiler): *nfa { - var n: *nfa; - var b: *nfa; - - n = parse_post(c); - if (!n) { - return nil; - } - - loop { - b = parse_post(c); - if (!b) { - return n; - } - - n = nfa_concat(c, n, b); - } -} - -// regex := concat -// | '|' regex -// | concat '|' regex -func parse_regex(c: *compiler): *nfa { - var n: *nfa; - var b: *nfa; - - n = parse_concat(c); - if (!n) { - n = nfa_empty(c); - } - - loop { - if (c.tt != T_ALT) { - return n; - } - feed(c); - - b = parse_concat(c); - if (!b) { - b = nfa_empty(c); - } - - n = nfa_alt(c, n, b); - } -} - -// decl := ident '=' regex ';' -func parse_decl(c: *compiler): *nfa { - var regex: *nfa; - var t: *tag; - var n: *nfa; - - t = parse_ident(c); - if (!t) { - return nil; - } - - if (c.tt != T_EQUALS) { - die("expected ="); - } - feed(c); - - regex = parse_regex(c); - - if (c.tt != T_SEMI) { - die("expected ;"); - } - feed(c); - - n = nfa_empty(c); - n.tag = t; - - return nfa_concat(c, regex, n); -} - -// progam := decl -// | decl program -func parse_program(c: *compiler): *nfa { - var n: *nfa; - var p: *nfa; - - p = nil; - loop { - n = parse_decl(c); - if (!n) { - if (c.tt != T_EOF) { - die("expected eof"); - } - return p; - } - - if (p) { - p = nfa_alt(c, p, n); - } else { - p = n; - } - } -} - -struct dfa { - id: int; - link: **dfa; - key: nlist; - l: *dfa; - r: *dfa; - seen: int; -} - -struct nlist { - live: **nfa; - fill: int; - cap: int; - tag: *tag; -} - -func alloc_nlist(c: *compiler, l: *nlist, cap: int): void { - l.cap = cap; - l.live = alloc(&c.a, sizeof(*l.live) * cap) as **nfa; - l.fill = 0; - l.tag = nil; -} - -func activate(l: *nlist, n: *nfa): void { - if (n.live) { - return; - } - - n.live = 1; - - l.live[l.fill] = n; - l.fill = l.fill + 1; - - if (n.left == -1) { - if ((!l.tag) || (n.tag && n.tag.id < l.tag.id)) { - l.tag = n.tag; - } - - if (n.a) { - activate(l, n.a); - } - - if (n.b) { - activate(l, n.b); - } - } -} - -func nlist_cmp(a: *nlist, b: *nlist): int { - var i: int; - - i = 0; - loop { - if (i == a.fill && i == b.fill) { - break; - } - - if (i == a.fill) { - return -1; - } - - if (i == b.fill) { - return 1; - } - - if (a.live[i].id > b.live[i].id) { - return 1; - } - - if (a.live[i].id < b.live[i].id) { - return -1; - } - - i = i + 1; - } - - if (!a.tag && !b.tag) { - return 0; - } - - if (!a.tag) { - return -1; - } - - if (!b.tag) { - return 1; - } - - if (a.tag.id < b.tag.id) { - return -1; - } - - if (a.tag.id > b.tag.id) { - return 1; - } - - return 0; -} - -func nlist_sort(l: *nlist): void { - var i: int; - var j: int; - var k: int; - var tmp: *nfa; - - if (l.fill < 2) { - return; - } - - i = l.fill - 1; - loop { - j = i; - loop { - k = (j << 1) + 1; - if (k >= l.fill) { - break; - } - if (k + 1 < l.fill && l.live[k].id < l.live[k + 1].id) { - k = k + 1; - } - if (l.live[j].id >= l.live[k].id) { - break; - } - tmp = l.live[k]; - l.live[k] = l.live[j]; - l.live[j] = tmp; - j = k; - } - if (i == 0) { - break; - } - i = i - 1; - } - - i = l.fill - 1; - loop { - if (i == 0) { - break; - } - tmp = l.live[i]; - l.live[i] = l.live[0]; - j = 0; - loop { - k = (j << 1) + 1; - if (k >= i) { - break; - } - if (k + 1 < i && l.live[k].id < l.live[k + 1].id) { - k = k + 1; - } - if (tmp.id >= l.live[k].id) { - break; - } - j = k; - } - l.live[j] = tmp; - i = i - 1; - } -} - -func alloc_link(c: *compiler): **dfa { - var link: **dfa; - var i: int; - link = alloc(&c.a, sizeof(*link) * 256) as **dfa; - i = 0; - loop { - if (i == 256) { - break; - } - link[i] = nil; - i = i + 1; - } - return link; -} - -func nlist_copy(c: *compiler, dest: *nlist, src: *nlist): void { - var i: int; - alloc_nlist(c, dest, src.fill); - dest.fill = src.fill; - dest.tag = src.tag; - i = 0; - loop { - if (i >= src.fill) { - break; - } - dest.live[i] = src.live[i]; - i = i + 1; - } -} - -func nlist2dfa(c: *compiler, l: *nlist): *dfa { - var link: **dfa; - var d: *dfa; - var n: *nfa; - var t: *tag; - var dir: int; - var i: int; - var j: int; - - if (l.fill == 0 && !l.tag) { - return nil; - } - - link = &c.d; - loop { - d = *link; - if (!d) { - break; - } - - dir = nlist_cmp(l, &d.key); - - if (dir < 0) { - link = &d.l; - } else if (dir > 0) { - link = &d.r; - } else { - return d; - } - } - - d = alloc(&c.a, sizeof(*d)) as *dfa; - d.id = c.ndfa; - d.link = alloc_link(c); - nlist_copy(c, &d.key, l); - d.l = nil; - d.r = nil; - d.seen = 0; - - c.ndfa = c.ndfa + 1; - - *link = d; - - i = 0; - loop { - if (i == 256) { - break; - } - - deactivate(l); - j = 0; - loop { - if (j >= d.key.fill) { - break; - } - - n = d.key.live[j]; - if (n.left <= i && i < n.right) { - if (n.a) { - activate(l, n.a); - } - if (n.b) { - activate(l, n.b); - } - } - - j = j + 1; - } - - d.link[i] = nlist2dfa(c, l); - - i = i + 1; - } - - return d; -} - -func deactivate(l: *nlist): void { - var i: int; - i = 0; - loop { - if (i >= l.fill) { - l.fill = 0; - l.tag = nil; - break; - } - l.live[i].live = 0; - i = i + 1; - } -} - -func powerset(c: *compiler, n: *nfa): *dfa { - var live: nlist; - alloc_nlist(c, &live, c.nnfa); - activate(&live, n); - return nlist2dfa(c, &live); -} - -func codegen(c: *compiler, a: *dfa): void { - var i: int; - var b: *dfa; - var lo: int; - var hi: int; - - if (a.seen) { - return; - } - a.seen = 1; - - fdputs(1, ":_"); - fdputd(1, a.id); - fdputs(1, ";\n"); - - if (a.key.tag) { - fdputs(1, "\tlexmark(l, T_"); - fdputs(1, a.key.tag.s); - fdputs(1, ");\n"); - } - - fdputs(1, "\tch = lexfeedc(l);\n"); - - i = 0; - loop { - loop { - if (i == 256 || a.link[i]) { - lo = i; - break; - } - i = i + 1; - } - - if (lo == 256) { - break; - } - - b = a.link[i]; - - loop { - if (i == 256 || a.link[i] != b) { - hi = i; - break; - } - i = i + 1; - } - - if (lo != (hi - 1)) { - fdputs(1, "\tif (ch >= "); - fdputd(1, lo); - fdputs(1, " && ch <= "); - fdputd(1, hi - 1); - } else { - fdputs(1, "\tif (ch == "); - fdputd(1, lo); - } - fdputs(1, ") { "); - fdputs(1, "goto _"); - fdputd(1, b.id); - fdputs(1, "; }\n"); - - } - - fdputs(1, "\treturn;\n"); - - i = 0; - loop { - if (i == 256) { - break; - } - - if (a.link[i]) { - codegen(c, a.link[i]); - } - - i = i + 1; - } -} - -func gen(c: *compiler, a: *dfa): void { - var t: *tag; - t = c.tags; - fdputs(1, "enum {\n"); - fdputs(1, "\tT_INVALID,\n"); - fdputs(1, "\tT_EOF,\n"); - loop { - if (!t) { - break; - } - fdputs(1, "\tT_"); - fdputs(1, t.s); - fdputs(1, ",\n"); - t = t.next; - } - fdputs(1, "}\n"); - fdputs(1, "\n"); - fdputs(1, "lexstep(l: *lex_state): void {\n"); - fdputs(1, "\tvar ch: int;\n"); - fdputs(1, "\tlexmark(l, T_INVALID);\n"); - codegen(c, a); - fdputs(1, "}\n"); -} - -func main(argc: int, argv: **byte, envp: **byte): void { - var c: compiler; - var n: *nfa; - var a: *dfa; - setup(&c); - n = parse_program(&c); - a = powerset(&c, n); - gen(&c, a); -} diff --git a/kernel.om b/kernel.om @@ -1,9 +1,5 @@ -func ud2(); - func syscall(n: int, a1: int, a2: int, a3: int, a4: int, a5: int, a6: int): int; -func cpuid(a: *int, c: *int, d: *int, b: *int); - func inb(a: int): int; func outb(a: int, x: int); @@ -16,14 +12,10 @@ func outd(a: int, x: int); func rdmsr(r: int): int; func wrmsr(r: int, x: int); -func rdcr0(): int; -func wrcr0(x: int); func rdcr2(): int; -func wrcr2(x: int); func rdcr3(): int; func wrcr3(x: int); func rdcr4(): int; -func wrcr4(x: int); func lgdt(base: *int, size: int); func lidt(base: *int, size: int); @@ -38,7 +30,6 @@ func sti(); func rdflags(): int; func wrflags(x: int); -func wbinvld(x: int); func invlpg(x: int); func _isr0(); diff --git a/lib.om b/lib.om @@ -1,7 +1,9 @@ func die(msg: *byte) { - fdputs(2, msg); - fdputs(2, "\n"); - exit(1); + var len: int; + len = strlen(msg); + write(2, msg, len); + write(2, "\n", 1); + exit(2); } func strlen(s: *byte): int { @@ -59,107 +61,6 @@ func strcmp(a: *byte, b: *byte): int { } } -func fdgetc(fd: int): int { - var b: byte; - var ret: int; - ret = read(fd, &b, 1); - if (ret == 1) { - return b as int; - } else if (ret == 0) { - return -1; - } else { - exit(3); - return 0; - } -} - -func fdputc(fd: int, ch: int) { - var b: byte; - var ret: int; - b = ch as byte; - ret = write(fd, &b, 1); - if (ret != 1) { - exit(3); - } -} - -func fdputs(fd: int, msg: *byte) { - var len: int; - var ret: int; - var off: int; - len = strlen(msg); - off = 0; - loop { - if (off == len) { - break; - } - ret = write(fd, msg, len - off); - if (ret < 0) { - exit(3); - } - off = off + ret; - } -} - -func fdputh(fd: int, n: int) { - var d: int; - - d = n & 15; - - n = n >> 4; - - if n { - fdputh(fd, n); - } - - fdputc(fd, "0123456789abcdef"[d] as int); -} - -func fdputhn(fd: int, x: int, d: int) { - loop { - if d == 0 { - break; - } - d = d - 4; - fdputc(fd, "0123456789abcdef"[(x >> d) & 15] as int); - } -} - -func fdputh8(fd: int, x: int) { - fdputhn(fd, x, 8); -} - -func fdputh16(fd: int, x: int) { - fdputhn(fd, x, 16); -} - -func fdputh32(fd: int, x: int) { - fdputhn(fd, x, 32); -} - -func fdputh64(fd: int, x: int) { - fdputhn(fd, x, 64); -} - -func fdputd(fd: int, n: int) { - var a: int; - - if (n < 0) { - fdputc(fd, '-'); - a = -(n % 10); - n = n / -10; - } else { - a = n % 10; - n = n / 10; - } - - if (n != 0) { - fdputd(fd, n); - } - - fdputc(fd, '0' + a); -} - func xxd_line(line: *byte, offset: int, data: *byte, len: int) { var i: int; var j: int; @@ -234,68 +135,6 @@ func xxd_line(line: *byte, offset: int, data: *byte, len: int) { line[i] = 0 as byte; } -func fdxxd(fd: int, data: *byte, len: int) { - var i: int; - var j: int; - loop { - if i >= len { - break; - } - - fdputh32(fd, i); - - fdputc(fd, ':'); - fdputc(fd, ' '); - - j = 0; - - loop { - if j == 16 { - break; - } - - if i + j < len { - fdputh8(fd, data[i + j] as int); - } else { - fdputc(fd, ' '); - fdputc(fd, ' '); - } - - if i + j + 1 < len { - fdputh8(fd, data[i + j + 1] as int); - } else { - fdputc(fd, ' '); - fdputc(fd, ' '); - } - - fdputc(fd, ' '); - - j = j + 2; - } - - fdputc(fd, ' '); - - j = 0; - loop { - if j == 16 || i + j >= len { - break; - } - - if data[i + j] as int >= 0x20 && data[i + j] as int < 0x80 { - fdputc(fd, data[i + j] as int); - } else { - fdputc(fd, '.'); - } - - j = j + 1; - } - - fdputc(fd, '\n'); - - i = i + 16; - } -} - func bzero(s: *byte, size: int) { var i: int; i = 0; diff --git a/ls.om b/ls.om @@ -4,10 +4,13 @@ func main(argc: int, argv: **byte, envp: **byte) { var n: int; var len: int; var a: alloc; + var out: *file; var buf: *byte; setup_alloc(&a); + out = fopen(1, &a); + fd = open(".", O_DIRECTORY, 0); if fd < 0 { exit(1); @@ -30,12 +33,12 @@ func main(argc: int, argv: **byte, envp: **byte) { break; } - fdputs(1, &buf[i + 19]); - fdputc(1, ' '); + fputs(out, &buf[i + 19]); + fputc(out, ' '); i = i + ((&buf[i + 16]) as *int[0] & 0xffff); } } - fdputc(1, '\n'); + fputc(out, '\n'); } diff --git a/parse2.om b/parse2.om @@ -15,7 +15,7 @@ func setup_parser(a: *alloc): *parser { return c; } -func parse(c: *parser, filename: *byte): *node { +func parse(c: *parser, filename: *byte, err: *file): *node { var f: *file; var fd: int; var len: int; @@ -24,9 +24,9 @@ func parse(c: *parser, filename: *byte): *node { fd = open(filename, 0, 0); if fd < 0 { - fdputs(2, "failed to open "); - fdputs(2, filename); - fdputs(2, "\n"); + fputs(err, "failed to open "); + fputs(err, filename); + fputs(err, "\n"); exit(1); } @@ -35,7 +35,7 @@ func parse(c: *parser, filename: *byte): *node { fclose(f); peg_reset(c.p, filename, src, len); - pn = peg_parse(c.p, P_sp); + pn = peg_parse(c.p, P_sp, err); return reconstruct(c, pn); } @@ -554,31 +554,6 @@ func reconstruct_args(c: *parser, pn: *peg_node): *node { return ret; } -func reconstruct_typeargs(c: *parser, pn: *peg_node): *node { - var ret: *node; - var link: **node; - var a: *node; - var n: *node; - - ret = nil; - link = &ret; - loop { - if !pn { - break; - } - - a = reconstruct_type(c, pn); - n = mknode1(c, N_TYPELIST, a); - copypos(n, pn); - - *link = n; - link = &n.b; - - pn = pn.next; - } - - return ret; -} func reconstruct_post(c: *parser, pn: *peg_node): *node { var ret: *node; diff --git a/peg.om b/peg.om @@ -364,7 +364,6 @@ func translate_pattern(c: *peg_compiler, n: *peg_node) { n = n.next; continue; } else { - fdputs(2, PEG_tag_to_str(n.tag)); die("invalid tag"); } @@ -473,7 +472,7 @@ func peg_open_output(c: *peg_compiler, filename: *byte) { c.out = f; } -func peg_compile(c: *peg_compiler, filename: *byte) { +func peg_compile(c: *peg_compiler, filename: *byte, err: *file) { var fd: int; var f: *file; var src: *byte; @@ -495,7 +494,7 @@ func peg_compile(c: *peg_compiler, filename: *byte) { c.p = peg_new(filename, src, len, c.a, peg_PEG_grammar, PEG_tag_to_str); - node = peg_parse(c.p, PEG_sp); + node = peg_parse(c.p, PEG_sp, err); translate(c, node); fflush(c.out); diff --git a/peglib.om b/peglib.om @@ -333,23 +333,23 @@ func peg_new(filename: *byte, src: *byte, len: int, a: *alloc, grammar: (func(c: return c; } -func peg_parse(c: *peg, sp: int): *peg_node { +func peg_parse(c: *peg, sp: int, err: *file): *peg_node { choice(c); if !c.grammar(c) { - fdputs(2, "syntax error at "); - fdputs(2, c.filename); - fdputs(2, ":"); - fdputd(2, c.fail_line); - fdputs(2, ":"); - fdputd(2, c.fail_col); - fdputs(2, " expected "); - fdputs(2, c.tag_to_str(c.fail_tag)); + fputs(err, "syntax error at "); + fputs(err, c.filename); + fputs(err, ":"); + fputd(err, c.fail_line); + fputs(err, ":"); + fputd(err, c.fail_col); + fputs(err, " expected "); + fputs(err, c.tag_to_str(c.fail_tag)); if c.fail_literal { - fdputs(2, " '"); - fdputs(2, c.fail_literal); - fdputs(2, "'"); + fputs(err, " '"); + fputs(err, c.fail_literal); + fputs(err, "'"); } - fdputs(2, "\n"); + fputs(err, "\n"); exit(1); } commit(c); diff --git a/sh.om b/sh.om @@ -29,6 +29,9 @@ enum { } func feedc(s: *shell) { + var b: byte; + var ret: int; + if s.script[0] { s.c = s.script[0] as int; s.script = &s.script[1]; @@ -36,7 +39,14 @@ func feedc(s: *shell) { } if s.scriptfd >= 0 { - s.c = fdgetc(s.scriptfd); + ret = read(s.scriptfd, &b, 1); + if ret == 1 { + s.c = b as int; + } else if ret == 0 { + s.c = -1; + } else { + exit(3); + } return; } diff --git a/type.om b/type.om @@ -81,22 +81,6 @@ func unify(c: *compiler, a: *type, b: *type) { } } -func count_args(c: *compiler, t: *type): int { - var nargs: int; - - nargs = 0; - loop { - if (!t) { - break; - } - - t = t.arg; - nargs = nargs + 1; - } - - return nargs; -} - func mktype(c: *compiler, kind: int, a: *type, b: *type, st: *decl): *type { var t: *type; diff --git a/unused.sh b/unused.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +diff <(sed -ne 's/^func \+\([^(]\+\)(.*/\1/p' *.om | sort -u) <(cat *.call | awk '{print $3}' | sort -u) | grep '<' | awk '{print $2}' | grep -v '_start' | grep -v '_kstart' diff --git a/xxd.om b/xxd.om @@ -1,4 +1,4 @@ -func doxxd(fd: int, buf: *byte) { +func doxxd(out: *file, fd: int, buf: *byte) { var n: int; var m: int; var ret: int; @@ -24,7 +24,7 @@ func doxxd(fd: int, buf: *byte) { } xxd_line(&buf[16], m, buf, n); - fdputs(1, &buf[16]); + fputs(out, &buf[16]); if n < 16 { break; @@ -39,16 +39,19 @@ func main(argc: int, argv: **byte, envp: **byte) { var i: int; var a: alloc; var buf: *byte; + var out: *file; setup_alloc(&a); + out = fopen(1, &a); + buf = alloc(&a, 4096); if argc == 1 { - doxxd(0, buf); + doxxd(out, 0, buf); } else if argc == 2 { fd = open(argv[1], 0, 0); - doxxd(fd, buf); + doxxd(out, fd, buf); close(fd); } else { die("usage: xxd [file]");