From afa258967288207bf0ce362fa26b6c0bebf75d95 Mon Sep 17 00:00:00 2001 From: Lobo Torres Date: Thu, 5 Dec 2024 12:50:29 -0300 Subject: [PATCH] gfjkfsdf --- LICENSE | 18 +++++-- Makefile | 9 +++- README.md | 6 +-- cmd/kj/main.ha | 5 ++ parse/+test/lexer.ha | 106 ++++++++++++++++++++++++++++++++++++ parse/lex.ha | 125 +++++-------------------------------------- test.bnm => test.kj | 0 7 files changed, 148 insertions(+), 121 deletions(-) create mode 100644 cmd/kj/main.ha create mode 100644 parse/+test/lexer.ha rename test.bnm => test.kj (100%) diff --git a/LICENSE b/LICENSE index 195940c..f361774 100644 --- a/LICENSE +++ b/LICENSE @@ -1,7 +1,19 @@ Copyright (c) 2024 Lobo Torres -Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: -The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/Makefile b/Makefile index 871dda7..94ce8fa 100644 --- a/Makefile +++ b/Makefile @@ -1,14 +1,19 @@ .POSIX: .SUFFIXES: + PREFIX=/usr/local SRCDIR=${PREFIX}/src HARESRCDIR=${SRCDIR}/hare HARE=hare HAREPATH=vendor/hare-unicode:${HARESRCDIR}/stdlib:${HARESRCDIR}/third-party -.PHONY: check build +.PHONY: check build clean check: @env HAREPATH=${HAREPATH} hare test -T +test + build: - @env HAREPATH=${HAREPATH} hare build + @env HAREPATH=${HAREPATH} hare build cmd/kj + +clean: + rm -f kj diff --git a/README.md b/README.md index f2e6195..07647ac 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ -# bnm -my silly catlang, partly inspired by [Scheme](https://r7rs.org/) and +# kojote + +the catlang that sings, partly inspired by [Scheme](https://r7rs.org/) and [Min](https://min-lang.org/). -currently _very_ work in progress :P diff --git a/cmd/kj/main.ha b/cmd/kj/main.ha new file mode 100644 index 0000000..c509971 --- /dev/null +++ b/cmd/kj/main.ha @@ -0,0 +1,5 @@ +use fmt; + +export fn main() void = { + fmt::println("Hello, world!")!; +}; diff --git a/parse/+test/lexer.ha b/parse/+test/lexer.ha new file mode 100644 index 0000000..bd2d359 --- /dev/null +++ b/parse/+test/lexer.ha @@ -0,0 +1,106 @@ +use memio; +use fmt; +use strings; +use io; + +@test fn lex() void = { + const cases: [_](str, []token) = [ + (`"hello" \greeting def`, + ["hello", mksym("greeting"), mkword("def")]), + (`[dup *] (a -- a) \square def`, + [quotstart, mkword("dup"), mkword("*"), quotend, + mkcomment("a -- a"), mksym("square"), + mkword("def")]), + (`#t #f`, [true, false]), + (`#\a #\space #\nul`, ['a', ' ', '\0']), + ]; + + for (let i = 0z; i < len(cases); i += 1) { + const src = strings::toutf8(cases[i].0); + const src = memio::fixed(src); + const lexer = newlexer(&src, ""); + defer close(&lexer); + + for (let j = 0z; j < len(cases[i].1); j += 1) { + const want = cases[i].1[j]; + const have = lex(&lexer)! as token; + + if (!tokeq(want, have)) { + fmt::printfln("Case {}: {}", i, cases[i].0)!; + fmt::print("\tExpected: ")!; + tokpp(want); + fmt::print("\tGot: ")!; + tokpp(have); + assert(false); + }; + }; + + assert(lex(&lexer) is io::EOF); + }; +}; + +fn tokeq(have: token, want: token) bool = { + match (want) { + case quotstart => + return have is quotstart; + case quotend => + return have is quotend; + case mapstart => + return have is mapstart; + case mapend => + return have is mapend; + case let w: word => + return (have as word).v == w.v; + case let s: str => + return have as str == s; + case let s: symbol => + return (have as symbol).v == s.v; + case let c: comment => + return (have as comment).v == c.v; + case let r: rune => + return have as rune == r; + case let b: bool => + return have as bool == b; + }; +}; + +fn tokpp(tok: token) void = { + match (tok) { + case quotstart => + fmt::println("[")!; + case quotend => + fmt::println("]")!; + case mapstart => + fmt::println("{")!; + case mapend => + fmt::println("}")!; + case let w: word => + fmt::println(w.v)!; + case let s: symbol => + fmt::printfln("{}{}", if (s.kw) ":" else "\\", s.v)!; + case let s: str => + fmt::printfln(`"{}"`, s)!; + case let c: comment => + fmt::printfln("({})", c.v)!; + case let r: rune => + for (let i = 0z; i < len(longcharnames); i += 1) { + if (r == longcharnames[i].1) { + fmt::printfln("#\\{}", longcharnames[i].0)!; + return; + }; + }; + fmt::printfln("#\\{}", r)!; + case let b: bool => + fmt::println(if (b) "#t" else "#f")!; + }; +}; + +fn mkword(v: const str) word = + word{ v = v }; + +fn mkcomment(v: const str) comment = + comment{ v = v }; + +fn mksym(v: const str, kw: bool = false) symbol = + symbol{ v = v, kw = kw }; + diff --git a/parse/lex.ha b/parse/lex.ha index 6fd4c7d..fe989ab 100644 --- a/parse/lex.ha +++ b/parse/lex.ha @@ -9,14 +9,12 @@ use strconv; use fmt; use strings; -// my cod prob sux :( - def longcharnames: [_](str, rune) = [ ("nul", '\u0000'), ("alarm", '\u0007'), ("backspace", '\u0008'), - ("newline", '\u000a'), ("tab", '\u0009'), + ("newline", '\u000a'), ("linefeed", '\u000a'), ("vtab", '\u000b'), ("page", '\u000c'), @@ -273,14 +271,17 @@ fn scanescape(lex: *lexer) (rune | error) = { }; switch (rn) { - case '"' => - return '"'; - case '\\' => - return '\\'; - case 'n' => - return '\n'; - case 't' => - return '\t'; + case '0' => return '\0'; + case 'a' => return '\a'; + case 'b' => return '\b'; + case 'e' => return '\x1b'; + case 'f' => return '\f'; + case 'n' => return '\n'; + case 'r' => return '\r'; + case 't' => return '\t'; + case 'v' => return '\v'; + case '\\' => return '\\'; + case '"' => return '"'; case => return lex.loc: invalid; }; @@ -307,105 +308,3 @@ fn isdelimiter(rn: rune) bool = { return false; }; }; - -@test fn lex() void = { - const cases: [_](str, []token) = [ - (`"hello" \greeting def`, - ["hello", mksym("greeting"), mkword("def")]), - (`[dup *] (a -- a) \square def`, - [quotstart, mkword("dup"), mkword("*"), quotend, - mkcomment("a -- a"), mksym("square"), - mkword("def")]), - (`#t #f`, [true, false]), - (`#\a #\space #\nul`, ['a', ' ', '\0']), - ]; - - for (let i = 0z; i < len(cases); i += 1) { - const src = strings::toutf8(cases[i].0); - const src = memio::fixed(src); - const lexer = newlexer(&src, ""); - defer close(&lexer); - - for (let j = 0z; j < len(cases[i].1); j += 1) { - const want = cases[i].1[j]; - const have = lex(&lexer)! as token; - - if (!tokeq(want, have)) { - fmt::printfln("Case {}: {}", i, cases[i].0)!; - fmt::print("\tExpected: ")!; - tokpprint(want); - fmt::print("\tGot: ")!; - tokpprint(have); - assert(false); - }; - }; - - assert(lex(&lexer) is io::EOF); - }; -}; - -fn tokeq(have: token, want: token) bool = { - match (want) { - case quotstart => - return have is quotstart; - case quotend => - return have is quotend; - case mapstart => - return have is mapstart; - case mapend => - return have is mapend; - case let w: word => - return (have as word).v == w.v; - case let s: str => - return have as str == s; - case let s: symbol => - return (have as symbol).v == s.v; - case let c: comment => - return (have as comment).v == c.v; - case let r: rune => - return have as rune == r; - case let b: bool => - return have as bool == b; - }; -}; - -fn tokpprint(tok: token) void = { - match (tok) { - case quotstart => - fmt::println("[")!; - case quotend => - fmt::println("]")!; - case mapstart => - fmt::println("{")!; - case mapend => - fmt::println("}")!; - case let w: word => - fmt::println(w.v)!; - case let s: symbol => - fmt::printfln("{}{}", if (s.kw) ":" else "\\", s.v)!; - case let s: str => - fmt::printfln(`"{}"`, s)!; - case let c: comment => - fmt::printfln("({})", c.v)!; - case let r: rune => - for (let i = 0z; i < len(longcharnames); i += 1) { - if (r == longcharnames[i].1) { - fmt::printfln("#\\{}", longcharnames[i].0)!; - return; - }; - }; - fmt::printfln("#\\{}", r)!; - case let b: bool => - fmt::println(if (b) "#t" else "#f")!; - }; -}; - -fn mkword(v: const str) word = - word{ v = v }; - -fn mkcomment(v: const str) comment = - comment{ v = v }; - -fn mksym(v: const str, kw: bool = false) symbol = - symbol{ v = v, kw = kw }; - diff --git a/test.bnm b/test.kj similarity index 100% rename from test.bnm rename to test.kj