Commit Graph

93 Commits

Author SHA1 Message Date
Aryadev Chavali
58069e083d Auto filled copyrightn otice in asm/base 2024-04-15 16:25:14 +06:30
Aryadev Chavali
958868714e main.cpp now preprocesses tokens and prints the output 2024-04-15 05:56:01 +06:30
Aryadev Chavali
e22ed450ac Fix some off by one errors 2024-04-15 05:34:52 +06:30
Aryadev Chavali
940dd2021e Fix issue with use_blocks not being preprocessed 2024-04-15 05:34:41 +06:30
Aryadev Chavali
7a6275c0a1 fix memory leak through vec.clear
vec.clear() doesn't delete pointers (unless they're smart) so I need
to do it myself.
2024-04-15 05:34:02 +06:30
Aryadev Chavali
8d3951a871 Implemented preprocesser function. 2024-04-15 05:08:55 +06:30
Aryadev Chavali
1e1a13e741 Default constructor for pp_err_t 2024-04-15 05:08:40 +06:30
Aryadev Chavali
0e5c934072 preprocess_* now uses const references to tokens
They copy and construct new token vectors and just read the token
inputs.
2024-04-15 05:08:07 +06:30
Aryadev Chavali
9ca93786af Updated main.cpp for changes to lexer 2024-04-15 05:07:16 +06:30
Aryadev Chavali
ec87245724 Implemented preprocess_const_blocks
Once again quite similar to preprocess_macro_blocks but shorter,
easier to use and easier to read. (76 vs 109)
2024-04-15 04:55:51 +06:30
Aryadev Chavali
81efc9006d Implement printing of pp_err_t
Another great thing for C++: the ability to tell it how to print
structures the way I want.  In C it's either:
1) Write a function to print the structure out (preferably to a file
pointer)
2) Write a function to return a string (allocated on the heap) which
represents it

Both are not fun to write, whereas it's much easier to write this.
2024-04-15 04:55:51 +06:30
Aryadev Chavali
929e5a3d0d Implement constructors for pp_err_t 2024-04-15 04:55:51 +06:30
Aryadev Chavali
0a93ad5a8a Implement preprocess_use_blocks
While being very similar in style to the C version, it takes 27 lines
of code less to implement it due to the niceties of C++ (41 lines vs
68).
2024-04-15 04:55:51 +06:30
Aryadev Chavali
f661438c93 Moved read_file to a general base library 2024-04-15 04:55:51 +06:30
Aryadev Chavali
0385d4bb8d Fix some off by one errors in lexer 2024-04-15 04:43:58 +06:30
Aryadev Chavali
f01d64b5f4 lexer now produces a vector of heap allocated tokens
This removes the problem of possibly expensive copies occurring due to
working with tokens produced from the lexer (that C++ just... does):
now we hold pointers where the copy operator is a lot easier to use.

I want expensive stuff to be done by me and for a reason: I want to
be holding the shotgun.
2024-04-15 04:42:24 +06:30
Aryadev Chavali
062ed12278 Rewrote preprocesser API
This C++ rewrite allows me to rewrite the actual API of the system.
In particular, I'm no longer restricting myself to just using enums
then figuring out a way to get proper error logging later down the
line (through tracking tokens in the buffer internally, for example).

Instead I can now design error structures which hold references to the
token they occurred on as well as possible lexical errors (if they're
a FILE_LEXICAL_ERROR which occurs due to the ~%USE~ macro).  This
means it's a lot easier to write error logging now at the top level.
2024-04-15 04:37:43 +06:30
Aryadev Chavali
72ef40e671 parser -> preprocesser + parser
I've decided to split the module parsing into two modules, one for the
preprocessing stage which only deals with tokens and the parsing stage
which generates bytecode.
2024-04-14 17:25:28 +06:30
Aryadev Chavali
86e9d51ab0 enum -> enum class in lexer
This makes enum elements scoped which is actually quite useful as I
prefer the namespacing that enum's give in C++.
2024-04-14 17:17:51 +06:30
Aryadev Chavali
86aca9a596 Added static assert to lexer in case of opcode changes 2024-04-14 17:12:24 +06:30
Aryadev Chavali
e5ef0292e7 asm/main now tokenises and prints the tokens of a given file
With error checking!
2024-04-14 17:11:48 +06:30
Aryadev Chavali
d368a49f56 Implemented a function to read a file in full
Uses std::optional in case file doesn't exist.
2024-04-14 17:10:23 +06:30
Aryadev Chavali
98d4f73134 asm/main now prints usage 2024-04-14 17:09:49 +06:30
Aryadev Chavali
44305138b0 Implemented cstr functions. 2024-04-14 17:05:56 +06:30
Aryadev Chavali
e55871195a Implemented overload for ostream and token as well as constructors for token 2024-04-14 17:05:52 +06:30
Aryadev Chavali
a8f605c89b Implemented tokenise_buffer
Note that this is basically the same as the previous version,
excluding the fact that it uses C++ idioms more and does a bit better
in error checking.
2024-04-14 17:04:15 +06:30
Aryadev Chavali
7a9e646d39 Implemented tokenise_literal_string
One thing I've realised is that even methods such as this require
error tracking.  I won't implement it in the tokenise method as it's
not related to consuming the string per se but instead in the main method.
2024-04-14 17:02:45 +06:30
Aryadev Chavali
50e9a4eef5 Implemented tokenise_literal_char (tokenise_char_literal)
I made the escape sequence parsing occur here instead of leaving it to
the main tokenise_buffer function as I think it's better suited here.
2024-04-14 17:01:35 +06:30
Aryadev Chavali
3c46fde66a Implemented tokenise_literal_hex
Note the overall size of this function in comparison to the C version,
as well as its clarity.

Of course, it is doing allocations in the background through
std::string which requires more profiling if I want to make this super
efficient™ but honestly the assembler just needs to work, whereas the
runtime needs to be fast.
2024-04-14 16:57:46 +06:30
Aryadev Chavali
4f8f511168 Implemented tokenise_literal_number (tokenise_number) 2024-04-14 16:56:58 +06:30
Aryadev Chavali
585aff1cbb Started implementing lexer in lexer.cpp
The implementation for tokenise_symbol is already a lot nicer to look
at and add to due to string/string_view operator overloading of ==.
Furthermore, error handling through pair<> instead of making some
custom structure which essentially does the same thing is already
making me happy for this rewrite.
2024-04-14 16:56:43 +06:30
Aryadev Chavali
e7a09c0de4 Wrote a new lexer API in C++
Essentially a refactor of the C formed lexer into C++ style.  I can
already see some benefits from doing this, in particular speed of
prototyping.
2024-04-14 16:52:58 +06:30
Aryadev Chavali
0ebbf3ca75 Start writing assembler in C++
Best language to use as it's already compatible with the headers I'm
using and can pretty neatly enter the build system while also using
the functions I've built for converting to and from bytecode!
2024-04-14 02:45:48 +06:30
Aryadev Chavali
4e9eb0a42e fix! loops in preprocess_use_blocks iterate to the wrong bound
A token_stream being constructed on the spot has different
used/available properties to a fully constructed one: a fully
constructed token stream uses available to hold the total number of
tokens and used as an internal iterator, while one that is still being
constructed uses the semantics of a standard darr.

Furthermore, some loops didn't divide by ~sizeof(token_t)~ which lead
to iteration over bound errors.
2024-04-14 02:00:17 +06:30
Aryadev Chavali
60588129b4 Cleaned up logs in assembler/parser 2023-11-29 23:09:51 +00:00
Aryadev Chavali
6a34fd2d2e Fixed incorrect free of tokens in error for preprocess_use_blocks
Also error now points to the correct place in the file.
2023-11-29 16:58:26 +00:00
Aryadev Chavali
fd1e6d96f6 Report some stats of the actual program when working 2023-11-29 15:46:44 +00:00
Aryadev Chavali
16dcc88a53 Refactored preprocessor to preprocess_(use|macro)_blocks and process_presults
We have distinct functions for the use blocks and the macro blocks,
which each generate wholesale new token streams via `token_copy` so we
don't run into weird errors around ownership of the internal strings
of each token.

Furthermore, process_presults now uses the stream index in each
presult to report errors when stuff goes wrong.
2023-11-29 15:43:53 +00:00
Aryadev Chavali
48d304056a Refactored presult_t to include a stream pointer
So when a presult_t is constructed it holds an index to where it was
constructed in terms of the token stream.  This will be useful when
implementing an error checker in the preprocessing or result parsing
stages.
2023-11-29 15:43:41 +00:00
Aryadev Chavali
4cee61fc9e Added parse errors for %USE calls
So %USE <STRING> is the expected call pattern, so there's an error if
there isn't a string after %USE.

The other two errors are file I/O errors i.e. nonexistent files or
errors in parsing the other file.  We don't report specifics about the
other file, that should be up to the user to check themselves.
2023-11-29 15:40:14 +00:00
Aryadev Chavali
9b8936ea02 Fixed tokenise_string_literal
Forgot to increment buffer->used and memcpy call was just incorrect.
2023-11-29 15:39:37 +00:00
Aryadev Chavali
ac70d4031c Added function to copy tokens
This essentially just copies the internal string of the token into a
new buffer.
2023-11-29 15:38:57 +00:00
Aryadev Chavali
1cba5ccd8d Added TOKEN_PP_USE to lexer with implementation 2023-11-29 15:38:41 +00:00
Aryadev Chavali
c9f684cc7d Added string literals in tokeniser
Doesn't do much, invalid for most operations.
2023-11-11 10:16:37 +00:00
Aryadev Chavali
cb2416554b Added a preprocessing routine in assembler
Preprocessor handles macros and macro blocks by working at the token
level, not doing any high level parsing or instruction making.
Essentially every macro is recorded in a registry, recording the name
and the tokens assigned to it.  Then for every caller it just inserts
the tokens inline, creating a new stream and freeing the old one.  It
leaves actual high level parsing to `parse_next` and
`process_presults`.
2023-11-08 18:15:26 +00:00
Aryadev Chavali
253bebb467 Added log in assembler for reading a certain number of bytes 2023-11-08 18:14:59 +00:00
Aryadev Chavali
642a8ae944 Lexer symbols now recognise macro constants and references 2023-11-08 18:14:41 +00:00
Aryadev Chavali
6e524569c3 Current work on preprocessor 2023-11-06 08:16:15 +00:00
Aryadev Chavali
4ae6c05276 Current work on preprocessor implementation
Lots to refactor and test
2023-11-05 16:21:09 +00:00
Aryadev Chavali
e9eead1177 Symbols may now include digits in lexer
This is mostly so labels get to have digits.  This won't affect number
tokens as that happens before symbols.
2023-11-03 21:50:55 +00:00