Compare commits
13 Commits
7e3dd2679d
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
00786d9cb9 | ||
|
|
b391fe9a74 | ||
|
|
12e82e64d0 | ||
|
|
9b55b9ec32 | ||
|
|
6545bd1302 | ||
|
|
c11b69092d | ||
|
|
82b96e23d5 | ||
|
|
0fdfbef1de | ||
|
|
6f6b747540 | ||
|
|
321b06ca0d | ||
|
|
d46ee32775 | ||
|
|
dc96e12145 | ||
|
|
42ac4f6bbb |
@@ -1,6 +1,6 @@
|
|||||||
;;; Directory Local Variables -*- no-byte-compile: t -*-
|
;;; Directory Local Variables -*- no-byte-compile: t -*-
|
||||||
;;; For more information see (info "(emacs) Directory Variables")
|
;;; For more information see (info "(emacs) Directory Variables")
|
||||||
|
|
||||||
((nil . ((compile-command . "make MODE=debug -k")
|
((nil . ((compile-command . "make -k MODE=debug examples")
|
||||||
(+license/license-choice . "MIT License")))
|
(+license/license-choice . "MIT License")))
|
||||||
(c-mode . ((mode . clang-format))))
|
(c-mode . ((mode . clang-format))))
|
||||||
|
|||||||
10
Makefile
10
Makefile
@@ -3,8 +3,8 @@ CC=cc
|
|||||||
DIST=build
|
DIST=build
|
||||||
OUT=$(DIST)/arl.out
|
OUT=$(DIST)/arl.out
|
||||||
|
|
||||||
MODULES=. lib parser
|
MODULES=$(shell cd include/arl; find . -type 'd' -printf "%f\n")
|
||||||
UNITS=main lib/vec lib/sv parser/ast parser/parser
|
UNITS=main cli lib/vec lib/sv lexer/token lexer/lexer
|
||||||
OBJECTS:=$(patsubst %,$(DIST)/%.o, $(UNITS))
|
OBJECTS:=$(patsubst %,$(DIST)/%.o, $(UNITS))
|
||||||
|
|
||||||
LDFLAGS=
|
LDFLAGS=
|
||||||
@@ -39,7 +39,7 @@ clangd: compile_commands.json
|
|||||||
compile_commands.json: Makefile
|
compile_commands.json: Makefile
|
||||||
bear -- $(MAKE) -B MODE=debug
|
bear -- $(MAKE) -B MODE=debug
|
||||||
|
|
||||||
.PHONY: run clean
|
.PHONY: run clean examples
|
||||||
ARGS=
|
ARGS=
|
||||||
run: $(OUT)
|
run: $(OUT)
|
||||||
./$^ $(ARGS)
|
./$^ $(ARGS)
|
||||||
@@ -47,5 +47,9 @@ run: $(OUT)
|
|||||||
clean:
|
clean:
|
||||||
rm -rf $(DIST)
|
rm -rf $(DIST)
|
||||||
|
|
||||||
|
examples: $(OUT)
|
||||||
|
@echo "Example: Hello World"
|
||||||
|
./$^ examples/hello-world.arl
|
||||||
|
|
||||||
DEPS:=$(patsubst %,$(DEPDIR)/%.d, $(UNITS))
|
DEPS:=$(patsubst %,$(DEPDIR)/%.d, $(UNITS))
|
||||||
include $(wildcard $(DEPS))
|
include $(wildcard $(DEPS))
|
||||||
|
|||||||
14
README
14
README
@@ -6,13 +6,12 @@
|
|||||||
│ /_/ \_\_| \_\_____| │
|
│ /_/ \_\_| \_\_____| │
|
||||||
└───────────────────────┘
|
└───────────────────────┘
|
||||||
|
|
||||||
Similar to Forth. Compiles to C.
|
Similar to Forth.
|
||||||
Native speed with simple semantics.
|
|
||||||
|
|
||||||
-----
|
-----
|
||||||
Goals
|
Goals
|
||||||
-----
|
-----
|
||||||
- Complete operational transpiler to C
|
- Complete operational transpiler, with C as a provisional working target
|
||||||
- Ability to reuse compiled code (as object code) in top level ARL code.
|
- Ability to reuse compiled code (as object code) in top level ARL code.
|
||||||
- Static type system with informative errors
|
- Static type system with informative errors
|
||||||
|
|
||||||
@@ -44,3 +43,12 @@ $ make DIST=<folder>
|
|||||||
|
|
||||||
Similarly, the general flags used in the C compiler may be set via the CFLAGS
|
Similarly, the general flags used in the C compiler may be set via the CFLAGS
|
||||||
variable, with linking arguments set via the LDFLAGS variable.
|
variable, with linking arguments set via the LDFLAGS variable.
|
||||||
|
|
||||||
|
------------------
|
||||||
|
Usage instructions
|
||||||
|
------------------
|
||||||
|
Once built, simply use the built binary like so:
|
||||||
|
$ ./build/arl.out <filename>
|
||||||
|
|
||||||
|
Alternatively, you can run the examples automatically via the Makefile:
|
||||||
|
$ make examples
|
||||||
195
arl.org
195
arl.org
@@ -1,161 +1,64 @@
|
|||||||
#+title: ARL - Issue tracker
|
#+title: ARL - Issue tracker
|
||||||
#+date: 2026-01-23
|
#+date: 2026-01-23
|
||||||
|
#+filetags: arl
|
||||||
|
|
||||||
* TODO Write a minimum working transpiler
|
* TODO Write a minimum working transpiler
|
||||||
We need to be able to compile the following file:
|
We need to be able to compile the following file:
|
||||||
[[file:examples/hello-world.arl]]. All it does is print "Hello,
|
[[file:examples/hello-world.arl]]. All it does is print "Hello,
|
||||||
world!". Should be relatively straightforward.
|
world!". Should be relatively straightforward.
|
||||||
|
** Stages
|
||||||
|
We need the following stages in our MVP transpiler:
|
||||||
|
- Source code reading (read bytes from a file)
|
||||||
|
- Parse raw bytes into tokens (Lexer)
|
||||||
|
- Interpret tokens into a classical AST (Parser)
|
||||||
|
- Stack effect and type analysis of the AST for soundness
|
||||||
|
- Translate AST into C code (Codegen)
|
||||||
|
- Compile C code into native executable (Target)
|
||||||
|
|
||||||
|
It's a Eulerian Path from the source code to the native executable.
|
||||||
** DONE Read file
|
** DONE Read file
|
||||||
** DONE Parser
|
** DONE Lexer
|
||||||
** TODO Intermediate representation (Virtual Machine)
|
[[file:src/lexer/]]
|
||||||
[[file:src/arl/vm/]]
|
[[file:include/arl/lexer/]]
|
||||||
|
** WIP Parser
|
||||||
|
[[file:src/parser/]]
|
||||||
|
[[file:include/arl/parser/]]
|
||||||
|
|
||||||
Before we get into generating C code and then compiling it, it might
|
We need to generate some form of AST from the token stream. This
|
||||||
be worth translating the parsed ARL code into a generic IR.
|
should be a little more advanced than our initial stream,
|
||||||
|
distinguishing between
|
||||||
|
- Literal values
|
||||||
|
- Primitive calls
|
||||||
|
- References to otherwise undefined words (may be defined through
|
||||||
|
import or later on)
|
||||||
|
** TODO Stack effect/type analysis
|
||||||
|
[[file:src/analysis/]]
|
||||||
|
[[file:include/arl/analysis/]]
|
||||||
|
|
||||||
The IR should be primitive in its semantics but should still
|
Given the AST, we need to verify the soundness of it with regards to
|
||||||
encapsulate the intention behind the original ARL code. This should
|
types and the stack. We have this idea of "stack effects" attached to
|
||||||
allow us to find a set of minimum requirements for target compilation:
|
every node in the AST; literals push values onto the stack and pop
|
||||||
- what can we reasonably use from the target platform to satisfy
|
nothing, while operations may pop some operands and push some values.
|
||||||
supporting the primitive IR?
|
|
||||||
- what do we need to hand-roll on the target in order to make this
|
|
||||||
work?
|
|
||||||
|
|
||||||
Essentially, we want to write a virtual machine, and translate ARL
|
We need a way to:
|
||||||
code into bytecode for that VM. Goals:
|
- Codify the stack effects of each type of AST node
|
||||||
- Type checking
|
- Infer the total stack effect from a sequence of nodes
|
||||||
- Optimiser (stretch)
|
|
||||||
|
|
||||||
We need the following clear items in our IR:
|
These stack effects work in tandem with our type analysis. Stack
|
||||||
- Static type values
|
shape analysis tells us what operands are being fed into primitives,
|
||||||
- Static type variables (possible DeBrujin numbering or other such
|
while the type analysis will tell us if the operands are well formed
|
||||||
mechanism to abstract naming away and leave it to the target to
|
for the primitives.
|
||||||
generate effectively)
|
|
||||||
- Strongly typed primitive operators (numeric, strings, I/O) with
|
|
||||||
packed arguments
|
|
||||||
|
|
||||||
We should have a rough grouping between AST objects and this IR. As
|
|
||||||
ARL is Forth-like, we can use the stack semantics to generate this IR
|
|
||||||
as we walk the AST in a linear manner. In practice this should almost
|
|
||||||
look like emulating a really small subset of the ARL language itself
|
|
||||||
and executing the program in that small subset.
|
|
||||||
|
|
||||||
Looking at how
|
|
||||||
[[https://en.wikipedia.org/wiki/Three-address_code][TAC]] works, I
|
|
||||||
think it may be a good idea to do something like that for our IR.
|
|
||||||
Essentially we should our AST into a sequence of really simple
|
|
||||||
bindings, with the final expression being a reference to some binding.
|
|
||||||
|
|
||||||
This also simplifies type checking to just verifying each little
|
|
||||||
binding and operation.
|
|
||||||
|
|
||||||
*** Examples
|
|
||||||
**** Basic example
|
|
||||||
Consider the following ARL code:
|
|
||||||
#+begin_src text
|
|
||||||
34 35 +
|
|
||||||
#+end_src
|
|
||||||
|
|
||||||
When we walk through the above code:
|
|
||||||
- 34 (an integer) is pushed onto the stack
|
|
||||||
- 35 (an integer) is pushed onto the stack
|
|
||||||
- ~+~ primitive is encountered
|
|
||||||
- Type check the top two values of the stack; they should be
|
|
||||||
integral.
|
|
||||||
- ~a b +~ should correspond to ~a + b~ so the IR expression should
|
|
||||||
pack the arguments in that order: ~prim-add(34,35)~.
|
|
||||||
- Bind the generated IR expression to some unique name, say ~v1~.
|
|
||||||
- Ensure this works with type checking; looking up ~v1~'s type
|
|
||||||
should give you the output type of the "+" operator (integer).
|
|
||||||
- Push ~v1~ onto the stack.
|
|
||||||
|
|
||||||
The final state of the stack should be something like ~[v1]~ where
|
|
||||||
~v1=prim-add(34,35)~. The final state of the stack, along with the
|
|
||||||
bindings we form, is the IR, to pass over to the later stages of the
|
|
||||||
compiler.
|
|
||||||
**** Slightly more complex example
|
|
||||||
Let's look at a slightly more complex program:
|
|
||||||
#+begin_src text
|
|
||||||
34 35 + 70 swap -
|
|
||||||
#+end_src
|
|
||||||
- 34 (integer) pushed
|
|
||||||
- 35 (integer) pushed
|
|
||||||
- ~+~ primitive:
|
|
||||||
- As stated previously, the final state of this primitive gives us
|
|
||||||
the name ~v1~ on the stack with the association
|
|
||||||
~v1=prim-add(34,35)~.
|
|
||||||
- 70 (integer) pushed
|
|
||||||
- ~swap~ primitive:
|
|
||||||
- Requires two values on the stack, but we care little about their
|
|
||||||
types. Just swaps their order on the stack.
|
|
||||||
- We /could/ introduce generics here to make the input/output
|
|
||||||
relation ship explicit (forall T, U swap:-(-> (T U) (U T))), but
|
|
||||||
at the same time we can just as easily get away with a type hole
|
|
||||||
(essentially some kind of ~any~). Up to debate.
|
|
||||||
- We do not generate IR for this primitive as it simply isn't
|
|
||||||
necessary. Instead we perform the swap on our IR stack and
|
|
||||||
continue. The ~swap~ primitive is "transparent" in the final IR.
|
|
||||||
- In this situation, the stack goes from ~[v1, 70]~ to
|
|
||||||
~[70, v1]~
|
|
||||||
- ~-~ primitive:
|
|
||||||
- Type checks the top two values of the stack (which are both
|
|
||||||
integers)
|
|
||||||
- ~a b -~ should correspond to ~a - b~, thus the corresponding IR
|
|
||||||
expression should be ~prim-sub(70,v1)~
|
|
||||||
- Associate IR expression with name ~v2~,
|
|
||||||
- Push ~v2~ onto the stack.
|
|
||||||
|
|
||||||
The final state of the IR should be:
|
|
||||||
- Stack: ~[v2]~
|
|
||||||
- Bindings:
|
|
||||||
- ~v1=prim-add(34,35)~
|
|
||||||
- ~v2=prim-sub(70,v1)~
|
|
||||||
|
|
||||||
Notice how some primitives generate IR, while others manipulate IR
|
|
||||||
themselves? They almost seem like macros!
|
|
||||||
|
|
||||||
Another thing of note is how the final state of the stack is a single
|
|
||||||
item in this case; an IR expression representing the entire program.
|
|
||||||
When we introduce code level bindings we won't have such nice outputs,
|
|
||||||
but it is certainly something to consider.
|
|
||||||
**** Hello world! example
|
|
||||||
For our hello world:
|
|
||||||
#+begin_src text
|
|
||||||
"Hello, world!\n" putstr
|
|
||||||
#+end_src
|
|
||||||
- "Hello, world!\n" (string) pushed
|
|
||||||
- "putstr" primitive:
|
|
||||||
- Type check the top of the stack (should be a string)
|
|
||||||
- Generate IR ~prim-putstr("Hello, world!\n")~
|
|
||||||
- Associate with name ~v1~ and push it onto the stack
|
|
||||||
|
|
||||||
Much simpler than our
|
|
||||||
*** TODO IR level type checking
|
|
||||||
During IR compilation, the following should be type checked:
|
|
||||||
- use of callables (primitives, user defined when implemented)
|
|
||||||
- variable assignment (when implemented)
|
|
||||||
- variable use (when implemented)
|
|
||||||
- definition of callables (when implemented)
|
|
||||||
|
|
||||||
We want to ensure no statement is unsound.
|
|
||||||
**** TODO Primitive types
|
|
||||||
Define the primitive types of the IR. Remember, simplicity is key,
|
|
||||||
but we need to mirror what we're getting on the ARL side.
|
|
||||||
**** TODO Type contracts for callables
|
|
||||||
Define how we can type check arguments on the stack against the types
|
|
||||||
a callable expects for its inputs. In the same vein, we also need to
|
|
||||||
figure out the type of whatever is pushed onto the stack by the
|
|
||||||
callable.
|
|
||||||
*** TODO Use SSA for user level bindings
|
|
||||||
[[https://en.wikipedia.org/wiki/Static_single-assignment_form][Static
|
|
||||||
single-assignment form]] is something we should use when we introduce
|
|
||||||
for user level bindings.
|
|
||||||
** TODO Code generator
|
** TODO Code generator
|
||||||
[[file:src/arl/target-c/]]
|
[[file:src/codegen/]]
|
||||||
|
[[file:include/arl/codegen/]]
|
||||||
|
|
||||||
This should take the IR translated from the AST generated by the
|
This should take the AST generated by the parser (which should already
|
||||||
parser, and write equivalent C code.
|
have been analysed), and write equivalent C code.
|
||||||
|
** TODO Target compilation
|
||||||
|
[[file:src/target/]]
|
||||||
|
[[file:include/arl/target/]]
|
||||||
|
|
||||||
After we've generated the C code, we need to call a C compiler on it
|
=gcc= and =clang= take C code via /stdin/, so we don't need to write
|
||||||
to generate a binary. GCC and Clang allow passing source code through
|
the C code to disk - we can just leave it as a buffer of bytes. So
|
||||||
stdin, so we don't even need to write to disk first which is nice.
|
we'll call the compilers and feed the generated code from the previous
|
||||||
|
stage into it via stdin.
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
"Hello, world!\n" putstr
|
"Hello, world!\n" puts
|
||||||
37
extensions/arl-mode.el
Normal file
37
extensions/arl-mode.el
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
;;; arl-mode.el --- ARL mode for Emacs -*- lexical-binding: t; -*-
|
||||||
|
|
||||||
|
;; Copyright (C) 2026 Aryadev Chavali
|
||||||
|
|
||||||
|
;; Author: Aryadev Chavali <aryadev@aryadevchavali.com>
|
||||||
|
;; Keywords:
|
||||||
|
|
||||||
|
;; Copyright (C) 2026 Aryadev Chavali
|
||||||
|
|
||||||
|
;; This program is distributed in the hope that it will be useful, but WITHOUT
|
||||||
|
;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||||
|
;; FOR A PARTICULAR PURPOSE. See the MIT License for details.
|
||||||
|
|
||||||
|
;; You may distribute and modify this code under the terms of the MIT License,
|
||||||
|
;; which you should have received a copy of along with this program. If not,
|
||||||
|
;; please go to <https://opensource.org/license/MIT>.
|
||||||
|
|
||||||
|
;;; Commentary:
|
||||||
|
|
||||||
|
;;
|
||||||
|
|
||||||
|
;;; Code:
|
||||||
|
|
||||||
|
(defvar arl-mode-comments '(?\; ";;" ("#|" . "|#")))
|
||||||
|
(defvar arl-mode-keywords '("if" "then" "else"))
|
||||||
|
(defvar arl-mode-expressions '(("\".*\"" . font-lock-string-face)))
|
||||||
|
(defvar arl-mode-automode-list '("\\.arl"))
|
||||||
|
|
||||||
|
(define-derived-mode arl-mode
|
||||||
|
arl-mode-comments
|
||||||
|
arl-mode-keywords
|
||||||
|
arl-mode-expressions
|
||||||
|
arl-mode-automode-list
|
||||||
|
nil)
|
||||||
|
|
||||||
|
(provide 'arl-mode)
|
||||||
|
;;; arl-mode.el ends here
|
||||||
31
include/arl/cli.h
Normal file
31
include/arl/cli.h
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
/* cli.h: CLI helpers
|
||||||
|
* Created: 2026-01-29
|
||||||
|
* Author: Aryadev Chavali
|
||||||
|
* License: See end of file
|
||||||
|
* Commentary:
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef CLI_H
|
||||||
|
#define CLI_H
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#include <arl/lib/sv.h>
|
||||||
|
|
||||||
|
int read_file(const char *filename, sv_t *ret);
|
||||||
|
int read_pipe(FILE *pipe, sv_t *ret);
|
||||||
|
void usage(FILE *fp);
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* Copyright (C) 2026 Aryadev Chavali
|
||||||
|
|
||||||
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||||
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||||
|
* FOR A PARTICULAR PURPOSE. See the MIT License for details.
|
||||||
|
|
||||||
|
* You may distribute and modify this code under the terms of the MIT License,
|
||||||
|
* which you should have received a copy of along with this program. If not,
|
||||||
|
* please go to <https://opensource.org/license/MIT>.
|
||||||
|
|
||||||
|
*/
|
||||||
@@ -1,38 +1,38 @@
|
|||||||
/* parser.h: Parser which takes character buffers and yields an AST
|
/* lexer.h: Lexer which takes character buffers and yields a sequence of tokens.
|
||||||
* Created: 2026-01-22
|
* Created: 2026-01-22
|
||||||
* Author: Aryadev Chavali
|
* Author: Aryadev Chavali
|
||||||
* License: See end of file
|
* License: See end of file
|
||||||
* Commentary:
|
* Commentary:
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef PARSER_H
|
#ifndef LEXER_H
|
||||||
#define PARSER_H
|
#define LEXER_H
|
||||||
|
|
||||||
#include <arl/parser/ast.h>
|
#include <arl/lexer/token.h>
|
||||||
|
|
||||||
/// Parser streams, utilised when generating an AST.
|
/// Token streams, utilised when lexing.
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
u64 byte;
|
u64 byte;
|
||||||
sv_t contents;
|
sv_t contents;
|
||||||
} parse_stream_t;
|
} lex_stream_t;
|
||||||
|
|
||||||
/// Types of errors that may occur during parsing
|
/// Types of errors that may occur during lexing
|
||||||
typedef enum
|
typedef enum
|
||||||
{
|
{
|
||||||
PARSE_ERR_OK = 0,
|
LEX_ERR_OK = 0,
|
||||||
PARSE_ERR_EXPECTED_SPEECH_MARKS,
|
LEX_ERR_EXPECTED_SPEECH_MARKS,
|
||||||
PARSE_ERR_UNKNOWN_CHAR,
|
LEX_ERR_UNKNOWN_CHAR,
|
||||||
} parse_err_t;
|
} lex_err_t;
|
||||||
const char *parse_err_to_string(parse_err_t err);
|
const char *lex_err_to_string(lex_err_t err);
|
||||||
|
|
||||||
// Generates an AST from STREAM, storing it in OUT. Returns any errors it may
|
// Generates a token stream from a lex_stream_t, storing it in OUT. Returns any
|
||||||
// generate.
|
// errors it may generate.
|
||||||
parse_err_t parse(ast_t *out, parse_stream_t *stream);
|
lex_err_t lex_stream(token_stream_t *out, lex_stream_t *stream);
|
||||||
|
|
||||||
// Computes the line and column that STREAM is currently pointing at in its
|
// Computes the line and column that STREAM is currently pointing at in its
|
||||||
// buffer, storing it in LINE and COL.
|
// buffer, storing it in LINE and COL.
|
||||||
void parse_stream_get_line_col(parse_stream_t *stream, u64 *line, u64 *col);
|
void lex_stream_get_line_col(lex_stream_t *stream, u64 *line, u64 *col);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
73
include/arl/lexer/token.h
Normal file
73
include/arl/lexer/token.h
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
/* token.h: General definition of tokens, and a sequence of them.
|
||||||
|
* Created: 2026-01-22
|
||||||
|
* Author: Aryadev Chavali
|
||||||
|
* License: See end of file
|
||||||
|
* Commentary:
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef TOKEN_H
|
||||||
|
#define TOKEN_H
|
||||||
|
|
||||||
|
#include <arl/lib/base.h>
|
||||||
|
#include <arl/lib/sv.h>
|
||||||
|
#include <arl/lib/vec.h>
|
||||||
|
|
||||||
|
/// Types of tokens
|
||||||
|
typedef enum
|
||||||
|
{
|
||||||
|
TOKEN_TYPE_KNOWN = 0,
|
||||||
|
TOKEN_TYPE_SYMBOL,
|
||||||
|
TOKEN_TYPE_STRING,
|
||||||
|
|
||||||
|
NUM_TOKEN_TYPES,
|
||||||
|
} token_type_t;
|
||||||
|
|
||||||
|
/// Known symbols which later stages would benefit from.
|
||||||
|
typedef enum
|
||||||
|
{
|
||||||
|
TOKEN_KNOWN_PUTS,
|
||||||
|
NUM_TOKEN_KNOWNS,
|
||||||
|
} token_known_t;
|
||||||
|
|
||||||
|
const char *token_known_to_cstr(token_known_t);
|
||||||
|
|
||||||
|
/// Tokens are a tagged union
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
u64 byte_location;
|
||||||
|
token_type_t type;
|
||||||
|
union
|
||||||
|
{
|
||||||
|
token_known_t as_known;
|
||||||
|
sv_t as_symbol;
|
||||||
|
sv_t as_string;
|
||||||
|
};
|
||||||
|
} token_t;
|
||||||
|
|
||||||
|
token_t token_known(u64 byte, token_known_t known);
|
||||||
|
token_t token_symbol(u64 byte, sv_t symbol);
|
||||||
|
token_t token_string(u64 byte, sv_t string);
|
||||||
|
void token_print(FILE *fp, token_t *token);
|
||||||
|
|
||||||
|
/// Sequence of tokens
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
vec_t vec;
|
||||||
|
} token_stream_t;
|
||||||
|
|
||||||
|
void token_stream_free(token_stream_t *token);
|
||||||
|
void token_stream_print(FILE *fp, token_stream_t *token);
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* Copyright (C) 2026 Aryadev Chavali
|
||||||
|
|
||||||
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||||
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||||
|
* FOR A PARTICULAR PURPOSE. See the MIT License for details.
|
||||||
|
|
||||||
|
* You may distribute and modify this code under the terms of the MIT License,
|
||||||
|
* which you should have received a copy of along with this program. If not,
|
||||||
|
* please go to <https://opensource.org/license/MIT>.
|
||||||
|
|
||||||
|
*/
|
||||||
@@ -31,9 +31,13 @@ typedef struct
|
|||||||
|
|
||||||
static_assert(sizeof(vec_t) == 64, "Expected sizeof(vec_t) to be 64");
|
static_assert(sizeof(vec_t) == 64, "Expected sizeof(vec_t) to be 64");
|
||||||
|
|
||||||
|
// standard old appending methods
|
||||||
void vec_append(vec_t *vec, const void *const ptr, u64 size);
|
void vec_append(vec_t *vec, const void *const ptr, u64 size);
|
||||||
void vec_append_byte(vec_t *vec, u8 byte);
|
void vec_append_byte(vec_t *vec, u8 byte);
|
||||||
|
|
||||||
|
// vector-as-a-stack
|
||||||
|
u8 *vec_pop(vec_t *vec, u64 size);
|
||||||
|
|
||||||
// Returns pointer to the start of the buffer VEC is currently using to store
|
// Returns pointer to the start of the buffer VEC is currently using to store
|
||||||
// data (either its inline buffer or the heap buffer).
|
// data (either its inline buffer or the heap buffer).
|
||||||
void *vec_data(vec_t *vec);
|
void *vec_data(vec_t *vec);
|
||||||
@@ -43,8 +47,13 @@ void vec_ensure_capacity(vec_t *vec, u64 capacity);
|
|||||||
|
|
||||||
// Ensure VEC has at least SIZE bytes free
|
// Ensure VEC has at least SIZE bytes free
|
||||||
void vec_ensure_free(vec_t *vec, u64 size);
|
void vec_ensure_free(vec_t *vec, u64 size);
|
||||||
|
|
||||||
|
// Free the memory associated with the vector
|
||||||
void vec_free(vec_t *vec);
|
void vec_free(vec_t *vec);
|
||||||
|
|
||||||
|
// Reset a vector while preserving any allocations
|
||||||
|
void vec_reset(vec_t *vec);
|
||||||
|
|
||||||
// Copy all data from V1 into V2.
|
// Copy all data from V1 into V2.
|
||||||
void vec_clone(vec_t *v2, vec_t *v1);
|
void vec_clone(vec_t *v2, vec_t *v1);
|
||||||
|
|
||||||
|
|||||||
@@ -1,74 +0,0 @@
|
|||||||
/* ast.h: General definition of the AST and nodes within it.
|
|
||||||
* Created: 2026-01-22
|
|
||||||
* Author: Aryadev Chavali
|
|
||||||
* License: See end of file
|
|
||||||
* Commentary:
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef AST_H
|
|
||||||
#define AST_H
|
|
||||||
|
|
||||||
#include <arl/lib/base.h>
|
|
||||||
#include <arl/lib/sv.h>
|
|
||||||
#include <arl/lib/vec.h>
|
|
||||||
|
|
||||||
/// Types the AST can encode
|
|
||||||
typedef enum
|
|
||||||
{
|
|
||||||
AST_NODE_TYPE_KNOWN = 0,
|
|
||||||
AST_NODE_TYPE_SYMBOL,
|
|
||||||
AST_NODE_TYPE_STRING,
|
|
||||||
|
|
||||||
NUM_AST_NODE_TYPES,
|
|
||||||
} ast_node_type_t;
|
|
||||||
|
|
||||||
/// Known symbols - may reference callables or values.
|
|
||||||
typedef enum
|
|
||||||
{
|
|
||||||
AST_KNOWN_PUTSTR,
|
|
||||||
|
|
||||||
NUM_AST_KNOWNS,
|
|
||||||
} ast_known_t;
|
|
||||||
|
|
||||||
const char *ast_known_to_cstr(ast_known_t);
|
|
||||||
|
|
||||||
/// Node of the AST as a tagged union
|
|
||||||
typedef struct
|
|
||||||
{
|
|
||||||
u64 byte_location;
|
|
||||||
ast_node_type_t type;
|
|
||||||
union
|
|
||||||
{
|
|
||||||
ast_known_t as_known;
|
|
||||||
sv_t as_symbol;
|
|
||||||
sv_t as_string;
|
|
||||||
};
|
|
||||||
} ast_node_t;
|
|
||||||
|
|
||||||
ast_node_t ast_node_known(u64 byte, ast_known_t known);
|
|
||||||
ast_node_t ast_node_symbol(u64 byte, sv_t symbol);
|
|
||||||
ast_node_t ast_node_string(u64 byte, sv_t string);
|
|
||||||
void ast_node_print(FILE *fp, ast_node_t *node);
|
|
||||||
|
|
||||||
/// The AST as a flat collection of nodes
|
|
||||||
typedef struct
|
|
||||||
{
|
|
||||||
vec_t nodes;
|
|
||||||
} ast_t;
|
|
||||||
|
|
||||||
void ast_free(ast_t *ast);
|
|
||||||
void ast_print(FILE *fp, ast_t *ast);
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/* Copyright (C) 2026 Aryadev Chavali
|
|
||||||
|
|
||||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
||||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
||||||
* FOR A PARTICULAR PURPOSE. See the MIT License for details.
|
|
||||||
|
|
||||||
* You may distribute and modify this code under the terms of the MIT License,
|
|
||||||
* which you should have received a copy of along with this program. If not,
|
|
||||||
* please go to <https://opensource.org/license/MIT>.
|
|
||||||
|
|
||||||
*/
|
|
||||||
83
src/cli.c
Normal file
83
src/cli.c
Normal file
@@ -0,0 +1,83 @@
|
|||||||
|
/* cli.c:
|
||||||
|
* Created: 2026-01-29
|
||||||
|
* Author: Aryadev Chavali
|
||||||
|
* License: See end of file
|
||||||
|
* Commentary: See /include/arl/cli.h
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include <arl/cli.h>
|
||||||
|
#include <arl/lib/vec.h>
|
||||||
|
|
||||||
|
int read_file(const char *filename, sv_t *ret)
|
||||||
|
{
|
||||||
|
// NOTE: Stupidly simple. Presumes the file is NOT three pipes in a trench
|
||||||
|
// coat.
|
||||||
|
FILE *fp = fopen(filename, "rb");
|
||||||
|
if (!fp)
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
fseek(fp, 0, SEEK_END);
|
||||||
|
ret->size = ftell(fp);
|
||||||
|
fseek(fp, 0, SEEK_SET);
|
||||||
|
ret->data = calloc(1, ret->size + 1);
|
||||||
|
fread(ret->data, ret->size, 1, fp);
|
||||||
|
fclose(fp);
|
||||||
|
|
||||||
|
ret->data[ret->size] = '\0';
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int read_pipe(FILE *pipe, sv_t *ret)
|
||||||
|
{
|
||||||
|
// NOTE: We can't read an entire pipe at once like we did for read_file. So
|
||||||
|
// let's read in buffered chunks, with a vector to keep them contiguous.
|
||||||
|
vec_t contents = {0};
|
||||||
|
char buffer[1024];
|
||||||
|
while (!feof(pipe))
|
||||||
|
{
|
||||||
|
size_t bytes_read = fread(buffer, 1, sizeof(buffer), pipe);
|
||||||
|
vec_append(&contents, buffer, bytes_read);
|
||||||
|
}
|
||||||
|
|
||||||
|
ret->size = contents.size;
|
||||||
|
// Get that null terminator in, but only after we've recorded the actual size
|
||||||
|
// of what's been read.
|
||||||
|
vec_append_byte(&contents, '\0');
|
||||||
|
|
||||||
|
if (contents.not_inlined)
|
||||||
|
{
|
||||||
|
// Take the heap pointer from us.
|
||||||
|
ret->data = vec_data(&contents);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// vec_data(&contents) is stack allocated; can't carry that out of this
|
||||||
|
// function!
|
||||||
|
ret->data = calloc(1, contents.size);
|
||||||
|
memcpy(ret->data, vec_data(&contents), contents.size);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void usage(FILE *fp)
|
||||||
|
{
|
||||||
|
fprintf(fp, "Usage: arl [FILE]\n"
|
||||||
|
"Compiles [FILE] as ARL source code.\n"
|
||||||
|
" [FILE]: File to compile.\n"
|
||||||
|
"If FILE is \"--\", then read from stdin.\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Copyright (C) 2026 Aryadev Chavali
|
||||||
|
|
||||||
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||||
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||||
|
* FOR A PARTICULAR PURPOSE. See the MIT License for details.
|
||||||
|
|
||||||
|
* You may distribute and modify this code under the terms of the MIT License,
|
||||||
|
* which you should have received a copy of along with this program. If not,
|
||||||
|
* please go to <https://opensource.org/license/MIT>.
|
||||||
|
|
||||||
|
*/
|
||||||
@@ -1,44 +1,44 @@
|
|||||||
/* parser.c: Implementation of parser.
|
/* lexr.c: Implementation of lexr.
|
||||||
* Created: 2026-01-22
|
* Created: 2026-01-22
|
||||||
* Author: Aryadev Chavali
|
* Author: Aryadev Chavali
|
||||||
* License: See end of file
|
* License: See end of file
|
||||||
* Commentary: See /include/arl/parser/parser.h
|
* Commentary: See /include/arl/lexer/lexer.h
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
#include <arl/lexer/lexer.h>
|
||||||
|
#include <arl/lexer/token.h>
|
||||||
#include <arl/lib/sv.h>
|
#include <arl/lib/sv.h>
|
||||||
#include <arl/parser/ast.h>
|
|
||||||
#include <arl/parser/parser.h>
|
|
||||||
|
|
||||||
/// Expected characters in a symbol
|
/// Expected characters in a symbol
|
||||||
static const char *SYMBOL_CHARS =
|
static const char *SYMBOL_CHARS =
|
||||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!#$%&'()*+,-./"
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!#$%&'()*+,-./"
|
||||||
":;<=>?@\\^_`{|}~0123456789";
|
":;<=>?@\\^_`{|}~0123456789";
|
||||||
|
|
||||||
const char *parse_err_to_string(parse_err_t err)
|
const char *lex_err_to_string(lex_err_t err)
|
||||||
{
|
{
|
||||||
switch (err)
|
switch (err)
|
||||||
{
|
{
|
||||||
case PARSE_ERR_OK:
|
case LEX_ERR_OK:
|
||||||
return "OK";
|
return "OK";
|
||||||
case PARSE_ERR_EXPECTED_SPEECH_MARKS:
|
case LEX_ERR_EXPECTED_SPEECH_MARKS:
|
||||||
return "EXPECTED_SPEECH_MARKS";
|
return "EXPECTED_SPEECH_MARKS";
|
||||||
case PARSE_ERR_UNKNOWN_CHAR:
|
case LEX_ERR_UNKNOWN_CHAR:
|
||||||
return "UNKNOWN_CHAR";
|
return "UNKNOWN_CHAR";
|
||||||
default:
|
default:
|
||||||
FAIL("Unexpected parse_err_t value: %d\n", err);
|
FAIL("Unexpected lex_err_t value: %d\n", err);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Prototypes for streams
|
/// Prototypes for streams
|
||||||
bool stream_eos(parse_stream_t *stream);
|
bool stream_eos(lex_stream_t *stream);
|
||||||
char stream_peek(parse_stream_t *stream);
|
char stream_peek(lex_stream_t *stream);
|
||||||
void stream_advance(parse_stream_t *stream, u64 size);
|
void stream_advance(lex_stream_t *stream, u64 size);
|
||||||
u64 stream_size(parse_stream_t *stream);
|
u64 stream_size(lex_stream_t *stream);
|
||||||
|
|
||||||
void parse_stream_get_line_col(parse_stream_t *stream, u64 *line, u64 *col)
|
void lex_stream_get_line_col(lex_stream_t *stream, u64 *line, u64 *col)
|
||||||
{
|
{
|
||||||
assert(stream && line && col && "Expected valid pointers.");
|
assert(stream && line && col && "Expected valid pointers.");
|
||||||
for (u64 i = 0; i < stream->byte; ++i)
|
for (u64 i = 0; i < stream->byte; ++i)
|
||||||
@@ -56,11 +56,11 @@ void parse_stream_get_line_col(parse_stream_t *stream, u64 *line, u64 *col)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Prototypes for parsing subroutines
|
/// Prototypes for lexing subroutines
|
||||||
parse_err_t parse_string(parse_stream_t *stream, ast_node_t *ret);
|
lex_err_t lex_string(lex_stream_t *stream, token_t *ret);
|
||||||
parse_err_t parse_symbol(parse_stream_t *stream, ast_node_t *ret);
|
lex_err_t lex_symbol(lex_stream_t *stream, token_t *ret);
|
||||||
|
|
||||||
parse_err_t parse(ast_t *out, parse_stream_t *stream)
|
lex_err_t lex_stream(token_stream_t *out, lex_stream_t *stream)
|
||||||
{
|
{
|
||||||
assert(out && stream && "Expected valid pointers");
|
assert(out && stream && "Expected valid pointers");
|
||||||
while (!stream_eos(stream))
|
while (!stream_eos(stream))
|
||||||
@@ -76,32 +76,32 @@ parse_err_t parse(ast_t *out, parse_stream_t *stream)
|
|||||||
}
|
}
|
||||||
else if (cur == '"')
|
else if (cur == '"')
|
||||||
{
|
{
|
||||||
// we make a copy for parse_string to mess with
|
// we make a copy for lex_string to mess with
|
||||||
ast_node_t ret = {0};
|
token_t ret = {0};
|
||||||
parse_err_t perr = parse_string(stream, &ret);
|
lex_err_t perr = lex_string(stream, &ret);
|
||||||
if (perr)
|
if (perr)
|
||||||
return perr;
|
return perr;
|
||||||
vec_append(&out->nodes, &ret, sizeof(ret));
|
vec_append(&out->vec, &ret, sizeof(ret));
|
||||||
}
|
}
|
||||||
else if (strchr(SYMBOL_CHARS, cur) && !isdigit(cur))
|
else if (strchr(SYMBOL_CHARS, cur) && !isdigit(cur))
|
||||||
{
|
{
|
||||||
// we make a copy for parse_symbol to mess with
|
// we make a copy for lex_symbol to mess with
|
||||||
ast_node_t ret = {0};
|
token_t ret = {0};
|
||||||
parse_err_t perr = parse_symbol(stream, &ret);
|
lex_err_t perr = lex_symbol(stream, &ret);
|
||||||
if (perr)
|
if (perr)
|
||||||
return perr;
|
return perr;
|
||||||
|
|
||||||
vec_append(&out->nodes, &ret, sizeof(ret));
|
vec_append(&out->vec, &ret, sizeof(ret));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return PARSE_ERR_UNKNOWN_CHAR;
|
return LEX_ERR_UNKNOWN_CHAR;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return PARSE_ERR_OK;
|
return LEX_ERR_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
parse_err_t parse_string(parse_stream_t *stream, ast_node_t *ret)
|
lex_err_t lex_string(lex_stream_t *stream, token_t *ret)
|
||||||
{
|
{
|
||||||
// Increment the cursor just past the first speechmark
|
// Increment the cursor just past the first speechmark
|
||||||
stream_advance(stream, 1);
|
stream_advance(stream, 1);
|
||||||
@@ -111,46 +111,46 @@ parse_err_t parse_string(parse_stream_t *stream, ast_node_t *ret)
|
|||||||
// If we're at the edge of the stream, there must not have been any
|
// If we're at the edge of the stream, there must not have been any
|
||||||
// speechmarks.
|
// speechmarks.
|
||||||
if (string.size + stream->byte == stream_size(stream))
|
if (string.size + stream->byte == stream_size(stream))
|
||||||
return PARSE_ERR_EXPECTED_SPEECH_MARKS;
|
return LEX_ERR_EXPECTED_SPEECH_MARKS;
|
||||||
|
|
||||||
// `string` is well defined, package and throw it back.
|
// `string` is well defined, package and throw it back.
|
||||||
*ret = ast_node_string(stream->byte - 1, string);
|
*ret = token_string(stream->byte - 1, string);
|
||||||
stream_advance(stream, string.size + 1);
|
stream_advance(stream, string.size + 1);
|
||||||
return PARSE_ERR_OK;
|
return LEX_ERR_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
parse_err_t parse_symbol(parse_stream_t *stream, ast_node_t *ret)
|
lex_err_t lex_symbol(lex_stream_t *stream, token_t *ret)
|
||||||
{
|
{
|
||||||
sv_t symbol = sv_chop_left(stream->contents, stream->byte);
|
sv_t symbol = sv_chop_left(stream->contents, stream->byte);
|
||||||
symbol.size = sv_while(symbol, SYMBOL_CHARS);
|
symbol.size = sv_while(symbol, SYMBOL_CHARS);
|
||||||
|
|
||||||
// see if symbol is one of the already known symbols
|
// see if symbol is one of the already known symbols
|
||||||
static_assert(NUM_AST_KNOWNS == 1, "Expected number of AST_KNOWNs");
|
static_assert(NUM_TOKEN_KNOWNS == 1, "Expected number of TOKEN_KNOWNs");
|
||||||
for (ast_known_t i = 0; i < NUM_AST_KNOWNS; ++i)
|
for (token_known_t i = 0; i < NUM_TOKEN_KNOWNS; ++i)
|
||||||
{
|
{
|
||||||
const char *possible_known = ast_known_to_cstr(i);
|
const char *possible_known = token_known_to_cstr(i);
|
||||||
if (strlen(possible_known) == symbol.size &&
|
if (strlen(possible_known) == symbol.size &&
|
||||||
strncmp(possible_known, symbol.data, symbol.size) == 0)
|
strncmp(possible_known, symbol.data, symbol.size) == 0)
|
||||||
{
|
{
|
||||||
// Found a matching known symbol
|
// Found a matching known symbol
|
||||||
*ret = ast_node_known(stream->byte, i);
|
*ret = token_known(stream->byte, i);
|
||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// otherwise, it must be a fresh symbol i.e. user defined
|
// otherwise, it must be a fresh symbol i.e. user defined
|
||||||
*ret = ast_node_symbol(stream->byte, symbol);
|
*ret = token_symbol(stream->byte, symbol);
|
||||||
end:
|
end:
|
||||||
stream_advance(stream, symbol.size);
|
stream_advance(stream, symbol.size);
|
||||||
return PARSE_ERR_OK;
|
return LEX_ERR_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool stream_eos(parse_stream_t *stream)
|
bool stream_eos(lex_stream_t *stream)
|
||||||
{
|
{
|
||||||
return stream->byte >= stream->contents.size;
|
return stream->byte >= stream->contents.size;
|
||||||
}
|
}
|
||||||
|
|
||||||
char stream_peek(parse_stream_t *stream)
|
char stream_peek(lex_stream_t *stream)
|
||||||
{
|
{
|
||||||
if (stream_eos(stream))
|
if (stream_eos(stream))
|
||||||
return '\0';
|
return '\0';
|
||||||
@@ -158,7 +158,7 @@ char stream_peek(parse_stream_t *stream)
|
|||||||
return stream->contents.data[stream->byte];
|
return stream->contents.data[stream->byte];
|
||||||
}
|
}
|
||||||
|
|
||||||
void stream_advance(parse_stream_t *stream, u64 size)
|
void stream_advance(lex_stream_t *stream, u64 size)
|
||||||
{
|
{
|
||||||
if (stream->byte + size >= stream->contents.size)
|
if (stream->byte + size >= stream->contents.size)
|
||||||
stream->byte = stream->contents.size;
|
stream->byte = stream->contents.size;
|
||||||
@@ -166,7 +166,7 @@ void stream_advance(parse_stream_t *stream, u64 size)
|
|||||||
stream->byte += size;
|
stream->byte += size;
|
||||||
}
|
}
|
||||||
|
|
||||||
u64 stream_size(parse_stream_t *stream)
|
u64 stream_size(lex_stream_t *stream)
|
||||||
{
|
{
|
||||||
return stream->contents.size;
|
return stream->contents.size;
|
||||||
}
|
}
|
||||||
115
src/lexer/token.c
Normal file
115
src/lexer/token.c
Normal file
@@ -0,0 +1,115 @@
|
|||||||
|
/* token.c: Implementation of TOKEN constructor/destructor functions
|
||||||
|
* Created: 2026-01-22
|
||||||
|
* Author: Aryadev Chavali
|
||||||
|
* License: See end of file
|
||||||
|
* Commentary: See /include/arl/lexer/token.h.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <arl/lexer/token.h>
|
||||||
|
#include <arl/lib/base.h>
|
||||||
|
#include <arl/lib/vec.h>
|
||||||
|
|
||||||
|
const char *token_known_to_cstr(token_known_t known)
|
||||||
|
{
|
||||||
|
switch (known)
|
||||||
|
{
|
||||||
|
case TOKEN_KNOWN_PUTS:
|
||||||
|
return "puts";
|
||||||
|
default:
|
||||||
|
FAIL("Unexpected TOKEN_KNOWN value: %d\n", known);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
token_t token_known(u64 byte, token_known_t known)
|
||||||
|
{
|
||||||
|
return (token_t){
|
||||||
|
.byte_location = byte,
|
||||||
|
.type = TOKEN_TYPE_KNOWN,
|
||||||
|
.as_known = known,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
token_t token_string(u64 byte, sv_t string)
|
||||||
|
{
|
||||||
|
return (token_t){
|
||||||
|
.byte_location = byte,
|
||||||
|
.type = TOKEN_TYPE_STRING,
|
||||||
|
.as_string = string,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
token_t token_symbol(u64 byte, sv_t symbol)
|
||||||
|
{
|
||||||
|
return (token_t){
|
||||||
|
.byte_location = byte,
|
||||||
|
.type = TOKEN_TYPE_SYMBOL,
|
||||||
|
.as_symbol = symbol,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
void token_print(FILE *fp, token_t *token)
|
||||||
|
{
|
||||||
|
if (!token)
|
||||||
|
{
|
||||||
|
fprintf(fp, "NIL");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
switch (token->type)
|
||||||
|
{
|
||||||
|
case TOKEN_TYPE_KNOWN:
|
||||||
|
fprintf(fp, "KNOWN(%s)", token_known_to_cstr(token->as_known));
|
||||||
|
break;
|
||||||
|
case TOKEN_TYPE_SYMBOL:
|
||||||
|
fprintf(fp, "SYMBOL(" PR_SV ")", SV_FMT(token->as_symbol));
|
||||||
|
break;
|
||||||
|
case TOKEN_TYPE_STRING:
|
||||||
|
fprintf(fp, "STRING(" PR_SV ")", SV_FMT(token->as_string));
|
||||||
|
break;
|
||||||
|
case NUM_TOKEN_TYPES:
|
||||||
|
default:
|
||||||
|
FAIL("Unexpected token type: %d\n", token->type);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void token_stream_print(FILE *fp, token_stream_t *token)
|
||||||
|
{
|
||||||
|
if (!token)
|
||||||
|
{
|
||||||
|
fprintf(fp, "{}");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
fprintf(fp, "{");
|
||||||
|
if (token->vec.size == 0)
|
||||||
|
{
|
||||||
|
fprintf(fp, "}\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
fprintf(fp, "\n");
|
||||||
|
for (u64 i = 0; i < token->vec.size / sizeof(token_t); ++i)
|
||||||
|
{
|
||||||
|
token_t item = VEC_GET(&token->vec, i, token_t);
|
||||||
|
fprintf(fp, "\t[%lu]: ", i);
|
||||||
|
token_print(fp, &item);
|
||||||
|
fprintf(fp, "\n");
|
||||||
|
}
|
||||||
|
fprintf(fp, "}");
|
||||||
|
}
|
||||||
|
|
||||||
|
void token_stream_free(token_stream_t *stream)
|
||||||
|
{
|
||||||
|
// we can free the vector itself and we're done
|
||||||
|
vec_free(&stream->vec);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Copyright (C) 2026 Aryadev Chavali
|
||||||
|
|
||||||
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||||
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||||
|
* FOR A PARTICULAR PURPOSE. See the MIT License for details.
|
||||||
|
|
||||||
|
* You may distribute and modify this code under the terms of the MIT License,
|
||||||
|
* which you should have received a copy of along with this program. If not,
|
||||||
|
* please go to <https://opensource.org/license/MIT>.
|
||||||
|
|
||||||
|
*/
|
||||||
@@ -2,7 +2,7 @@
|
|||||||
* Created: 2026-01-22
|
* Created: 2026-01-22
|
||||||
* Author: Aryadev Chavali
|
* Author: Aryadev Chavali
|
||||||
* License: See end of file
|
* License: See end of file
|
||||||
* Commentary:
|
* Commentary: See /include/arl/lib/vec.h
|
||||||
|
|
||||||
Taken from prick_vec.h: see https://github.com/oreodave/prick.
|
Taken from prick_vec.h: see https://github.com/oreodave/prick.
|
||||||
*/
|
*/
|
||||||
@@ -31,6 +31,14 @@ void vec_append_byte(vec_t *vec, u8 byte)
|
|||||||
++vec->size;
|
++vec->size;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
u8 *vec_pop(vec_t *vec, u64 size)
|
||||||
|
{
|
||||||
|
if (!vec || vec->size < size)
|
||||||
|
return NULL;
|
||||||
|
vec->size -= size;
|
||||||
|
return (u8 *)vec_data(vec) + vec->size;
|
||||||
|
}
|
||||||
|
|
||||||
void *vec_data(vec_t *vec)
|
void *vec_data(vec_t *vec)
|
||||||
{
|
{
|
||||||
if (!vec)
|
if (!vec)
|
||||||
@@ -89,6 +97,14 @@ void vec_free(vec_t *vec)
|
|||||||
memset(vec, 1, sizeof(*vec));
|
memset(vec, 1, sizeof(*vec));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void vec_reset(vec_t *vec)
|
||||||
|
{
|
||||||
|
if (!vec)
|
||||||
|
return;
|
||||||
|
memset(vec_data(vec), 0, vec->capacity);
|
||||||
|
vec->size = 0;
|
||||||
|
}
|
||||||
|
|
||||||
void vec_clone(vec_t *v2, vec_t *v1)
|
void vec_clone(vec_t *v2, vec_t *v1)
|
||||||
{
|
{
|
||||||
if (!v1 || !v2)
|
if (!v1 || !v2)
|
||||||
|
|||||||
82
src/main.c
82
src/main.c
@@ -12,70 +12,13 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
#include <arl/lexer/lexer.h>
|
||||||
|
#include <arl/lexer/token.h>
|
||||||
#include <arl/lib/base.h>
|
#include <arl/lib/base.h>
|
||||||
#include <arl/lib/sv.h>
|
#include <arl/lib/sv.h>
|
||||||
#include <arl/lib/vec.h>
|
#include <arl/lib/vec.h>
|
||||||
#include <arl/parser/ast.h>
|
|
||||||
#include <arl/parser/parser.h>
|
|
||||||
|
|
||||||
int read_file(const char *filename, sv_t *ret)
|
#include <arl/cli.h>
|
||||||
{
|
|
||||||
// NOTE: Stupidly simple. Presumes the file is NOT three pipes in a trench
|
|
||||||
// coat.
|
|
||||||
FILE *fp = fopen(filename, "rb");
|
|
||||||
if (!fp)
|
|
||||||
return 1;
|
|
||||||
|
|
||||||
fseek(fp, 0, SEEK_END);
|
|
||||||
ret->size = ftell(fp);
|
|
||||||
fseek(fp, 0, SEEK_SET);
|
|
||||||
ret->data = calloc(1, ret->size + 1);
|
|
||||||
fread(ret->data, ret->size, 1, fp);
|
|
||||||
fclose(fp);
|
|
||||||
|
|
||||||
ret->data[ret->size] = '\0';
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int read_pipe(FILE *pipe, sv_t *ret)
|
|
||||||
{
|
|
||||||
// NOTE: We can't read an entire pipe at once like we did for read_file. So
|
|
||||||
// let's read in buffered chunks, with a vector to keep them contiguous.
|
|
||||||
vec_t contents = {0};
|
|
||||||
char buffer[1024];
|
|
||||||
while (!feof(pipe))
|
|
||||||
{
|
|
||||||
size_t bytes_read = fread(buffer, 1, sizeof(buffer), pipe);
|
|
||||||
vec_append(&contents, buffer, bytes_read);
|
|
||||||
}
|
|
||||||
|
|
||||||
ret->size = contents.size;
|
|
||||||
// Get that null terminator in, but only after we've recorded the actual size
|
|
||||||
// of what's been read.
|
|
||||||
vec_append_byte(&contents, '\0');
|
|
||||||
|
|
||||||
if (contents.not_inlined)
|
|
||||||
{
|
|
||||||
// Take the heap pointer from us.
|
|
||||||
ret->data = vec_data(&contents);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// vec_data(&contents) is stack allocated; can't carry that out of this
|
|
||||||
// function!
|
|
||||||
ret->data = calloc(1, contents.size);
|
|
||||||
memcpy(ret->data, vec_data(&contents), contents.size);
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
void usage(FILE *fp)
|
|
||||||
{
|
|
||||||
fprintf(fp, "Usage: arl [FILE]\n"
|
|
||||||
"Compiles [FILE] as ARL source code.\n"
|
|
||||||
" [FILE]: File to compile.\n"
|
|
||||||
"If FILE is \"--\", then read from stdin.\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
@@ -114,30 +57,29 @@ int main(int argc, char *argv[])
|
|||||||
|
|
||||||
LOG("%s => `" PR_SV "`\n", filename, SV_FMT(contents));
|
LOG("%s => `" PR_SV "`\n", filename, SV_FMT(contents));
|
||||||
|
|
||||||
parse_stream_t stream = {.byte = 0, .contents = contents};
|
lex_stream_t stream = {.byte = 0, .contents = contents};
|
||||||
ast_t ast = {0};
|
token_stream_t tokens = {0};
|
||||||
parse_err_t perr = parse(&ast, &stream);
|
lex_err_t perr = lex_stream(&tokens, &stream);
|
||||||
if (perr)
|
if (perr)
|
||||||
{
|
{
|
||||||
u64 line = 1, col = 0;
|
u64 line = 1, col = 0;
|
||||||
parse_stream_get_line_col(&stream, &line, &col);
|
lex_stream_get_line_col(&stream, &line, &col);
|
||||||
|
|
||||||
LOG_ERR("%s:%lu:%lu: %s\n", filename, line, col, parse_err_to_string(perr));
|
LOG_ERR("%s:%lu:%lu: %s\n", filename, line, col, lex_err_to_string(perr));
|
||||||
ret = 1;
|
ret = 1;
|
||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
|
|
||||||
LOG("Parsed %lu nodes\n", ast.nodes.size / sizeof(ast_node_t));
|
|
||||||
#if VERBOSE_LOGS
|
#if VERBOSE_LOGS
|
||||||
ast_print(stdout, &ast);
|
LOG("Lexed %lu tokens ", tokens.vec.size / sizeof(token_t));
|
||||||
#endif
|
token_stream_print(stdout, &tokens);
|
||||||
printf("\n");
|
printf("\n");
|
||||||
|
#endif
|
||||||
|
|
||||||
end:
|
end:
|
||||||
if (contents.data)
|
if (contents.data)
|
||||||
free(contents.data);
|
free(contents.data);
|
||||||
if (ast.nodes.capacity > 0)
|
token_stream_free(&tokens);
|
||||||
ast_free(&ast);
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
115
src/parser/ast.c
115
src/parser/ast.c
@@ -1,115 +0,0 @@
|
|||||||
/* ast.c: Implementation of AST constructor/destructor functions
|
|
||||||
* Created: 2026-01-22
|
|
||||||
* Author: Aryadev Chavali
|
|
||||||
* License: See end of file
|
|
||||||
* Commentary: See /include/arl/parser/ast.h.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <arl/lib/base.h>
|
|
||||||
#include <arl/lib/vec.h>
|
|
||||||
#include <arl/parser/ast.h>
|
|
||||||
|
|
||||||
const char *ast_known_to_cstr(ast_known_t known)
|
|
||||||
{
|
|
||||||
switch (known)
|
|
||||||
{
|
|
||||||
case AST_KNOWN_PUTSTR:
|
|
||||||
return "putstr";
|
|
||||||
default:
|
|
||||||
FAIL("Unexpected AST_KNOWN value: %d\n", known);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ast_node_t ast_node_known(u64 byte, ast_known_t known)
|
|
||||||
{
|
|
||||||
return (ast_node_t){
|
|
||||||
.byte_location = byte,
|
|
||||||
.type = AST_NODE_TYPE_KNOWN,
|
|
||||||
.as_known = known,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
ast_node_t ast_node_string(u64 byte, sv_t string)
|
|
||||||
{
|
|
||||||
return (ast_node_t){
|
|
||||||
.byte_location = byte,
|
|
||||||
.type = AST_NODE_TYPE_STRING,
|
|
||||||
.as_string = string,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
ast_node_t ast_node_symbol(u64 byte, sv_t symbol)
|
|
||||||
{
|
|
||||||
return (ast_node_t){
|
|
||||||
.byte_location = byte,
|
|
||||||
.type = AST_NODE_TYPE_SYMBOL,
|
|
||||||
.as_symbol = symbol,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
void ast_node_print(FILE *fp, ast_node_t *node)
|
|
||||||
{
|
|
||||||
if (!node)
|
|
||||||
{
|
|
||||||
fprintf(fp, "NIL");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
switch (node->type)
|
|
||||||
{
|
|
||||||
case AST_NODE_TYPE_KNOWN:
|
|
||||||
fprintf(fp, "KNOWN(%s)", ast_known_to_cstr(node->as_known));
|
|
||||||
break;
|
|
||||||
case AST_NODE_TYPE_SYMBOL:
|
|
||||||
fprintf(fp, "SYMBOL(" PR_SV ")", SV_FMT(node->as_symbol));
|
|
||||||
break;
|
|
||||||
case AST_NODE_TYPE_STRING:
|
|
||||||
fprintf(fp, "STRING(" PR_SV ")", SV_FMT(node->as_string));
|
|
||||||
break;
|
|
||||||
case NUM_AST_NODE_TYPES:
|
|
||||||
default:
|
|
||||||
FAIL("Unexpected node type: %d\n", node->type);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void ast_print(FILE *fp, ast_t *ast)
|
|
||||||
{
|
|
||||||
if (!ast)
|
|
||||||
{
|
|
||||||
fprintf(fp, "{}");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
fprintf(fp, "{");
|
|
||||||
if (ast->nodes.size == 0)
|
|
||||||
{
|
|
||||||
fprintf(fp, "}\n");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
fprintf(fp, "\n");
|
|
||||||
for (u64 i = 0; i < ast->nodes.size / sizeof(ast_node_t); ++i)
|
|
||||||
{
|
|
||||||
ast_node_t item = VEC_GET(&ast->nodes, i, ast_node_t);
|
|
||||||
fprintf(fp, "\t[%lu]: ", i);
|
|
||||||
ast_node_print(fp, &item);
|
|
||||||
fprintf(fp, "\n");
|
|
||||||
}
|
|
||||||
fprintf(fp, "}");
|
|
||||||
}
|
|
||||||
|
|
||||||
void ast_free(ast_t *ast)
|
|
||||||
{
|
|
||||||
// we can free the vector itself and we're done
|
|
||||||
vec_free(&ast->nodes);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Copyright (C) 2026 Aryadev Chavali
|
|
||||||
|
|
||||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
||||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
||||||
* FOR A PARTICULAR PURPOSE. See the MIT License for details.
|
|
||||||
|
|
||||||
* You may distribute and modify this code under the terms of the MIT License,
|
|
||||||
* which you should have received a copy of along with this program. If not,
|
|
||||||
* please go to <https://opensource.org/license/MIT>.
|
|
||||||
|
|
||||||
*/
|
|
||||||
Reference in New Issue
Block a user