From beccf376ecbcc3819cc0d102b3635b6e5f39fa9e Mon Sep 17 00:00:00 2001 From: Syndamia Date: Sat, 1 Nov 2025 17:30:08 +0200 Subject: feat: Output token list --- src/StringConsumer/StringConsumerEngine.cl | 6 ++++ src/Token/TokenList.cl | 56 ++++++++++++++++++++++++++++++ src/main.cl | 1 + 3 files changed, 63 insertions(+) create mode 100644 src/Token/TokenList.cl (limited to 'src') diff --git a/src/StringConsumer/StringConsumerEngine.cl b/src/StringConsumer/StringConsumerEngine.cl index 6fe4403..6f07d2e 100644 --- a/src/StringConsumer/StringConsumerEngine.cl +++ b/src/StringConsumer/StringConsumerEngine.cl @@ -4,6 +4,7 @@ class StringConsumerEngine inherits System { ; str: String; + tl: TokenList <- new TokenList; init(code: String) : Object {{ assert(not (code = ""), "init", "input code cannot be empty!"); @@ -23,6 +24,7 @@ class StringConsumerEngine inherits System { case consumers.find(str) of c: IStringConsumer => { str <- str.substr(c.consumed_length(), str.length() - c.consumed_length()); + tl.push(c.token()); true; }; o: Object => false; @@ -37,4 +39,8 @@ class StringConsumerEngine inherits System { empty() : Bool { str = "" }; + + generated_tokens() : TokenList { + tl + }; }; diff --git a/src/Token/TokenList.cl b/src/Token/TokenList.cl new file mode 100644 index 0000000..514a8e6 --- /dev/null +++ b/src/Token/TokenList.cl @@ -0,0 +1,56 @@ +class TokenList inherits System { + token: IToken; + next: TokenList; + + init(value: IToken, tail: Object) : TokenList {{ + assert(not (isvoid value), "init", "value argument cannot be void!"); + assert(isvoid token, "init", "List has already been initialised!"); + + token <- value; + if isvoid tail then + void + else + case tail of + tl: TokenList => next <- tl; + o: Object => assert(false, "init", "tail argument can be either a TokenList or void!"); + esac + fi; + + self; + }}; + + push(value: IToken) : TokenList {{ + assert(not (isvoid value), "push", "value argument cannot be void!"); + + if isvoid token + then + token <- value + else if isvoid next + then + next <- (new TokenList).init(value, void) + else + next.push(value) + fi + fi; + self; + }}; + + out() : Object {{ + if isvoid token + then + void + else + out_string("<") + .out_string(token.name()) + .out_string(">") + fi; + if isvoid next + then + void + else { + out_string(" "); + next.out(); + } + fi; + }}; +}; diff --git a/src/main.cl b/src/main.cl index 4040444..3b839a9 100644 --- a/src/main.cl +++ b/src/main.cl @@ -7,5 +7,6 @@ class Main inherits System { loop assert(sce.try_consume(), "main", "couldn't consume entire input!") pool; + sce.generated_tokens().out(); }}; }; -- cgit v1.3.1-23-gd82d