bldc/include/symrepr.h

260 lines
8.2 KiB
C
Raw Normal View History

/*
Copyright 2018, 2021, 2022 Joel Svensson svenssonjoel@yahoo.se
2022 Benjamin Vedder
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
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
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/** \file symrepr.h
* symrepr.h implements the symbol table and symbol creation and lookup functions.
*
* The symbol table is implemented as a linked list in the arrays and symbols
* memory defined in lispbm_memory.h. So lbm_memory_init must be run before
* the symbol table is initialized and used.
*
*/
#ifndef SYMTAB_H_
#define SYMTAB_H_
#include <stdint.h>
#include <stdbool.h>
#include "lbm_types.h"
// Default and fixed symbol ids
#define SYM_NIL 0x0
#define SYM_QUOTE 0x1
#define SYM_TRUE 0x2
#define SYM_IF 0x3
#define SYM_LAMBDA 0x4
#define SYM_CLOSURE 0x5
#define SYM_LET 0x6
#define SYM_DEFINE 0x7
#define SYM_PROGN 0x8
#define SYM_READ 0x9
#define SYM_READ_PROGRAM 0xA
#define SYM_DONTCARE 0xB
#define SYM_MATCH 0xC
#define SYM_SEND 0xD
#define SYM_RECEIVE 0xE
#define SYM_MACRO 0xF
#define SYM_MACRO_EXPAND 0x10
#define SYM_CALLCC 0x11
#define SYM_CONT 0x12
#define SYM_SETVAR 0x13
// 0x20 - 0x2F are errors
#define SYM_RERROR 0x20 /* READ ERROR */
#define SYM_TERROR 0x21 /* TYPE ERROR */
#define SYM_EERROR 0x22 /* EVAL ERROR */
#define SYM_MERROR 0x23
#define SYM_NOT_FOUND 0x24
#define SYM_DIVZERO 0x25
#define SYM_FATAL_ERROR 0x26 /* Runtime system is corrupt */
#define SYM_STACK_ERROR 0x27
#define SYM_RECOVERED 0x28
Squashed 'lispBM/lispBM/' changes from 6199703d..43ebce71 43ebce71 Fix formatting int print.c and constants that default to float/double fa220f0b update gc statistics collection a small bit 25b20686 fix get_heap_state 783d774e Arrays of 64bit values passes the associated tests. More tests are needed though ff40d02f working on 64bit arrays. more testing needed 204bea39 CMP as a macro instead of 8 different functions 44bb152d added a set of math extensions grabbed from Benjamin's bldc repo 9c34b4cd added string extensions from benjamins bldc repo b4370155 merge in changes on master into dev64bit c129344b fixed masking bug in fundamentals ed3ab5be closed down ome warnings when building 64bit d46564c9 expanded functionality of type-of to cover 64bit types and preparations for arrays of 64bit values f7cb5538 added more 64bit functionality to fundamentals and a tiny amount of tests 4dafbe6c added decoders for 64bit values 63026a8a added 64bit value literals 8c1f0f0f made GC aware of lbm_memory allocated values 17148ada 64 bit values allocated on lbm_memory on 32 bit version 4445e5a8 sketch of encoders for 64 bit values f636e64d suffix for the lisp size of int and uint are now i and u instead of i28 and u28 840723ca preparations and planning for 64bit types in both 32 and 64 bit versions. 13675dda 64bit up and running. but there are many TODOs 033bfd9a small fixes following bug hunt f5c984de fix bug in representation of important masks and constants d40b2437 Merge branch 'master' into dev64bit 06f9603f Merge branch 'master' into dev64bit 69950ba1 32 bit tests are ok 46b6fa28 Merge branch 'master' into dev64bit b9e2c993 work in progress 8dff9b4d work in progress a241aded work in progress 4738e0da update year in lbm_types ca469923 made it possible to run the same tests as on the 32 bit version on the 64 bit code 85acec30 small tweaks tp create_ctx, use correct type bf0286a7 Merge branch 'master' into dev64bit 824e1634 Merge branch 'master' into dev64bit 04a97f17 lots of testing needed ea862cce a bunch of changes in preparation for 64bit compatibility. git-subtree-dir: lispBM/lispBM git-subtree-split: 43ebce71a281f4a158b8d2dec3fc537c26766ec3
2022-03-25 07:47:05 -07:00
#define TYPE_CLASSIFIER_STARTS 0x30
#define SYM_ARRAY_TYPE 0x30
Squashed 'lispBM/lispBM/' changes from 6199703d..43ebce71 43ebce71 Fix formatting int print.c and constants that default to float/double fa220f0b update gc statistics collection a small bit 25b20686 fix get_heap_state 783d774e Arrays of 64bit values passes the associated tests. More tests are needed though ff40d02f working on 64bit arrays. more testing needed 204bea39 CMP as a macro instead of 8 different functions 44bb152d added a set of math extensions grabbed from Benjamin's bldc repo 9c34b4cd added string extensions from benjamins bldc repo b4370155 merge in changes on master into dev64bit c129344b fixed masking bug in fundamentals ed3ab5be closed down ome warnings when building 64bit d46564c9 expanded functionality of type-of to cover 64bit types and preparations for arrays of 64bit values f7cb5538 added more 64bit functionality to fundamentals and a tiny amount of tests 4dafbe6c added decoders for 64bit values 63026a8a added 64bit value literals 8c1f0f0f made GC aware of lbm_memory allocated values 17148ada 64 bit values allocated on lbm_memory on 32 bit version 4445e5a8 sketch of encoders for 64 bit values f636e64d suffix for the lisp size of int and uint are now i and u instead of i28 and u28 840723ca preparations and planning for 64bit types in both 32 and 64 bit versions. 13675dda 64bit up and running. but there are many TODOs 033bfd9a small fixes following bug hunt f5c984de fix bug in representation of important masks and constants d40b2437 Merge branch 'master' into dev64bit 06f9603f Merge branch 'master' into dev64bit 69950ba1 32 bit tests are ok 46b6fa28 Merge branch 'master' into dev64bit b9e2c993 work in progress 8dff9b4d work in progress a241aded work in progress 4738e0da update year in lbm_types ca469923 made it possible to run the same tests as on the 32 bit version on the 64 bit code 85acec30 small tweaks tp create_ctx, use correct type bf0286a7 Merge branch 'master' into dev64bit 824e1634 Merge branch 'master' into dev64bit 04a97f17 lots of testing needed ea862cce a bunch of changes in preparation for 64bit compatibility. git-subtree-dir: lispBM/lispBM git-subtree-split: 43ebce71a281f4a158b8d2dec3fc537c26766ec3
2022-03-25 07:47:05 -07:00
#define SYM_RAW_I_TYPE 0x31
#define SYM_RAW_U_TYPE 0x32
#define SYM_RAW_F_TYPE 0x33
#define SYM_IND_I_TYPE 0x34
#define SYM_IND_U_TYPE 0x35
#define SYM_IND_F_TYPE 0x36
#define SYM_STREAM_TYPE 0x37
#define SYM_BYTECODE_TYPE 0x38
#define TYPE_CLASSIFIER_ENDS 0x38
#define SYM_NONSENSE 0x39
#define SYM_NO_MATCH 0x3A
#define SYM_MATCH_ANY 0x3B
#define SYM_MATCH_I 0x3C
#define SYM_MATCH_U 0x3D
#define SYM_MATCH_U32 0x3E
#define SYM_MATCH_I32 0x3F
#define SYM_MATCH_FLOAT 0x40
#define SYM_MATCH_CONS 0x41
// Type identifying symbols
#define SYM_TYPE_LIST 0x50
Squashed 'lispBM/lispBM/' changes from 6199703d..43ebce71 43ebce71 Fix formatting int print.c and constants that default to float/double fa220f0b update gc statistics collection a small bit 25b20686 fix get_heap_state 783d774e Arrays of 64bit values passes the associated tests. More tests are needed though ff40d02f working on 64bit arrays. more testing needed 204bea39 CMP as a macro instead of 8 different functions 44bb152d added a set of math extensions grabbed from Benjamin's bldc repo 9c34b4cd added string extensions from benjamins bldc repo b4370155 merge in changes on master into dev64bit c129344b fixed masking bug in fundamentals ed3ab5be closed down ome warnings when building 64bit d46564c9 expanded functionality of type-of to cover 64bit types and preparations for arrays of 64bit values f7cb5538 added more 64bit functionality to fundamentals and a tiny amount of tests 4dafbe6c added decoders for 64bit values 63026a8a added 64bit value literals 8c1f0f0f made GC aware of lbm_memory allocated values 17148ada 64 bit values allocated on lbm_memory on 32 bit version 4445e5a8 sketch of encoders for 64 bit values f636e64d suffix for the lisp size of int and uint are now i and u instead of i28 and u28 840723ca preparations and planning for 64bit types in both 32 and 64 bit versions. 13675dda 64bit up and running. but there are many TODOs 033bfd9a small fixes following bug hunt f5c984de fix bug in representation of important masks and constants d40b2437 Merge branch 'master' into dev64bit 06f9603f Merge branch 'master' into dev64bit 69950ba1 32 bit tests are ok 46b6fa28 Merge branch 'master' into dev64bit b9e2c993 work in progress 8dff9b4d work in progress a241aded work in progress 4738e0da update year in lbm_types ca469923 made it possible to run the same tests as on the 32 bit version on the 64 bit code 85acec30 small tweaks tp create_ctx, use correct type bf0286a7 Merge branch 'master' into dev64bit 824e1634 Merge branch 'master' into dev64bit 04a97f17 lots of testing needed ea862cce a bunch of changes in preparation for 64bit compatibility. git-subtree-dir: lispBM/lispBM git-subtree-split: 43ebce71a281f4a158b8d2dec3fc537c26766ec3
2022-03-25 07:47:05 -07:00
#define SYM_TYPE_I 0x51
#define SYM_TYPE_U 0x52
#define SYM_TYPE_FLOAT 0x53
#define SYM_TYPE_I32 0x54
#define SYM_TYPE_U32 0x55
Squashed 'lispBM/lispBM/' changes from 6199703d..43ebce71 43ebce71 Fix formatting int print.c and constants that default to float/double fa220f0b update gc statistics collection a small bit 25b20686 fix get_heap_state 783d774e Arrays of 64bit values passes the associated tests. More tests are needed though ff40d02f working on 64bit arrays. more testing needed 204bea39 CMP as a macro instead of 8 different functions 44bb152d added a set of math extensions grabbed from Benjamin's bldc repo 9c34b4cd added string extensions from benjamins bldc repo b4370155 merge in changes on master into dev64bit c129344b fixed masking bug in fundamentals ed3ab5be closed down ome warnings when building 64bit d46564c9 expanded functionality of type-of to cover 64bit types and preparations for arrays of 64bit values f7cb5538 added more 64bit functionality to fundamentals and a tiny amount of tests 4dafbe6c added decoders for 64bit values 63026a8a added 64bit value literals 8c1f0f0f made GC aware of lbm_memory allocated values 17148ada 64 bit values allocated on lbm_memory on 32 bit version 4445e5a8 sketch of encoders for 64 bit values f636e64d suffix for the lisp size of int and uint are now i and u instead of i28 and u28 840723ca preparations and planning for 64bit types in both 32 and 64 bit versions. 13675dda 64bit up and running. but there are many TODOs 033bfd9a small fixes following bug hunt f5c984de fix bug in representation of important masks and constants d40b2437 Merge branch 'master' into dev64bit 06f9603f Merge branch 'master' into dev64bit 69950ba1 32 bit tests are ok 46b6fa28 Merge branch 'master' into dev64bit b9e2c993 work in progress 8dff9b4d work in progress a241aded work in progress 4738e0da update year in lbm_types ca469923 made it possible to run the same tests as on the 32 bit version on the 64 bit code 85acec30 small tweaks tp create_ctx, use correct type bf0286a7 Merge branch 'master' into dev64bit 824e1634 Merge branch 'master' into dev64bit 04a97f17 lots of testing needed ea862cce a bunch of changes in preparation for 64bit compatibility. git-subtree-dir: lispBM/lispBM git-subtree-split: 43ebce71a281f4a158b8d2dec3fc537c26766ec3
2022-03-25 07:47:05 -07:00
#define SYM_TYPE_DOUBLE 0x56
#define SYM_TYPE_I64 0x57
#define SYM_TYPE_U64 0x58
#define SYM_TYPE_ARRAY 0x59
#define SYM_TYPE_SYMBOL 0x5A
#define SYM_TYPE_CHAR 0x5B
#define SYM_TYPE_BYTE 0x5C
#define SYM_TYPE_REF 0x5D
#define SYM_TYPE_STREAM 0x5E
//Relevant for the tokenizer
#define SYM_OPENPAR 0x70
#define SYM_CLOSEPAR 0x71
#define SYM_BACKQUOTE 0x72
#define SYM_COMMA 0x73
#define SYM_COMMAAT 0x74
#define SYM_TOKENIZER_DONE 0x75
#define SYM_DOT 0x76
#define SYM_QUOTE_IT 0x77
// Fundamental Operations
#define FUNDAMENTALS_START 0x100
#define SYM_ADD 0x100
#define SYM_SUB 0x101
#define SYM_MUL 0x102
#define SYM_DIV 0x103
#define SYM_MOD 0x104
#define SYM_EQ 0x105
#define SYM_NUMEQ 0x106
#define SYM_LT 0x107
#define SYM_GT 0x108
#define SYM_LEQ 0x109
#define SYM_GEQ 0x10A
#define SYM_EVAL 0x10B
#define SYM_EVAL_PROGRAM 0x10C
#define SYM_PERFORM_GC 0x10D
#define SYM_AND 0x110
#define SYM_OR 0x111
#define SYM_NOT 0x112
#define SYM_YIELD 0x113
#define SYM_WAIT 0x114
#define SYM_SPAWN 0x115
#define SYM_CONS 0x120
#define SYM_CAR 0x121
#define SYM_CDR 0x122
#define SYM_LIST 0x123
#define SYM_APPEND 0x124
#define SYM_ARRAY_READ 0x130
#define SYM_ARRAY_WRITE 0x131
Squashed 'lispBM/lispBM/' changes from 6a219e20..7930b985 7930b985 tweak change log e8869883 small amounts of optimization to eval_cps ac3bd152 silly call-cc test 8dba90e3 Merge branch 'master' into dev f4cf6a54 added one more test of array_extensions, packing and upacking floats b27895cb added test for hex numbers 337c0eda fix bug in compression that applies to hexadecimal numnbers. a55c5857 added another array_extensions test and fixed one bug d255d69b added another array_extensions test 983024d4 array creation from lisp and one test added 65d8a498 bug fix array_extensions 47ddb8ae updates to array_extensions cb5c3421 small tweaks to array-extensions, starting to maybe make sense 01233e92 some small additions to array_extensions fb9db7ab mini-tweak array_extensions a05aa268 fix bugs in lispbm.mk and add a test of variables2 ddca1665 small tweaks array-extensions 2466abc6 work in progress 0dc7ec7c work in progress with a library of array extensions df142ceb added .clang_complete 5229ca5e planning, no code yet cd6f5e3f Merge branch 'master' into dev 0f4c9bf2 added a rule for running tests from the makefile and tweaks test-script to only fail if there are unexpected failures 7a61f4e1 Merge branch 'master' into dev 9f8c9caf update lbm_version feab23d3 Merge branch 'master' into dev 112c01df added array creation from C on the lbm_memory. GC will remove these ca70c70a Merge branch 'master' into dev f8e38cc2 Merge branch 'master' into dev fc203669 Merge branch 'master' into dev 57af7182 Merge branch 'master' into dev 037e2352 commenting and version 977cd92a Merge branch 'master' into dev 34322bcf small tweak readme 1f22da9e fix recursion issue with macros fec81a7b version tweak 8548884b macros and callcc 7eadf3cc added a macro test2 8152930d improvement to macro subsystem cce45228 first sketch of macro expansion git-subtree-dir: lispBM/lispBM git-subtree-split: 7930b9854a05a8d09b40d078da94bb3743ef977b
2022-02-21 03:02:15 -08:00
#define SYM_ARRAY_CREATE 0x132
#define SYM_SYMBOL_TO_STRING 0x140
#define SYM_STRING_TO_SYMBOL 0x141
#define SYM_SYMBOL_TO_UINT 0x142
#define SYM_UINT_TO_SYMBOL 0x143
#define SYM_SET_CAR 0x145
#define SYM_SET_CDR 0x146
#define SYM_IS_FUNDAMENTAL 0x150
#define SYM_IX 0x151
#define SYM_ENCODE_I32 0x152
#define SYM_ENCODE_U32 0x153
#define SYM_ENCODE_FLOAT 0x154
#define SYM_DECODE 0x155
//#define SYM_STREAM_GET 0x160
//#define SYM_STREAM_MORE 0x161
//#define SYM_STREAM_PEEK 0x162
//#define SYM_STREAM_DROP 0x163
//#define SYM_STREAM_PUT 0x164
#define SYM_SHL 0x170
#define SYM_SHR 0x171
#define SYM_BITWISE_AND 0x172
#define SYM_BITWISE_OR 0x173
#define SYM_BITWISE_XOR 0x174
#define SYM_BITWISE_NOT 0x175
#define SYM_TYPE_OF 0x200
#define FUNDAMENTALS_END 0x200
#define SPECIAL_SYMBOLS_START 0
#define SPECIAL_SYMBOLS_END 0xFFFF
#define EXTENSION_SYMBOLS_START 0x10000
#define EXTENSION_SYMBOLS_END 0x1FFFF
#define VARIABLE_SYMBOLS_START 0x20000
#define VARIABLE_SYMBOLS_END 0x2FFFF
#define RUNTIME_SYMBOLS_START 0x30000
#define MAX_SYMBOL_VALUE 0x0FFFFFFF
typedef void (*symrepr_name_iterator_fun)(const char *);
/** Initialize the symbol table.
*
* \return 1
*/
extern int lbm_symrepr_init(void);
/** Iterate over all symbol names as strings
*
* \param symrepr_name_iterator_fun function taking a string
*/
extern void lbm_symrepr_name_iterator(symrepr_name_iterator_fun f);
/** Add a symbol to the symbol table. The symbol name string is copied to arrays and symbols memory.
*
* \param name String representation of the symbol.
* \param id Resulting id is returned through this argument.
* \return 1 for success and 0 for failure.
*/
extern int lbm_add_symbol(char *name, lbm_uint *id);
/** Add a variable-symbol to the symbol table. The symbol name string is copied to arrays and symbols memory.
*
* \param name String representation of the symbol.
* \param id Resulting id is returned through this argument.
* \return 1 for success and 0 for failure.
*/
extern int lbm_add_variable_symbol(char *name, lbm_uint* id);
/** Add a symbol to the symbol table. The name is assumed to be a statically allocated string.
*
* \param name Statically allocated name string.
* \param id Resulting id is returned through this argument.
* \return 1 for success and 0 for failure.
*/
extern int lbm_add_symbol_const(char *name, lbm_uint *id);
/** Add an extension symbol to the symbol table.
* The name is assumed to be statically allocated.
*
* \param name Statically allocated name string.
* \param id Resulting id is returned through this argument.
* \return 1 for success and 0 for failure.
*/
extern int lbm_add_extension_symbol_const(char *name, lbm_uint* id);
/** Look up an id from the symbol table given a name.
*
* \param name Name string to look up.
* \param id Resulting id is returned through this argument.
* \return 1 on success (name was found) and 0 for failure.
*/
extern int lbm_get_symbol_by_name(char *name, lbm_uint *id);
/** Look up a symbol name from the symbol table given an id.
*
* \param id The id to look up in the symbol table.
* \return pointer to the name string if success otherwise NULL.
*/
extern const char* lbm_get_name_by_symbol(lbm_uint id);
extern int lbm_get_num_variables(void);
/**
*
* \return The amount of space occupied by the symbol table in bytes.
*/
Squashed 'lispBM/lispBM/' changes from 6199703d..43ebce71 43ebce71 Fix formatting int print.c and constants that default to float/double fa220f0b update gc statistics collection a small bit 25b20686 fix get_heap_state 783d774e Arrays of 64bit values passes the associated tests. More tests are needed though ff40d02f working on 64bit arrays. more testing needed 204bea39 CMP as a macro instead of 8 different functions 44bb152d added a set of math extensions grabbed from Benjamin's bldc repo 9c34b4cd added string extensions from benjamins bldc repo b4370155 merge in changes on master into dev64bit c129344b fixed masking bug in fundamentals ed3ab5be closed down ome warnings when building 64bit d46564c9 expanded functionality of type-of to cover 64bit types and preparations for arrays of 64bit values f7cb5538 added more 64bit functionality to fundamentals and a tiny amount of tests 4dafbe6c added decoders for 64bit values 63026a8a added 64bit value literals 8c1f0f0f made GC aware of lbm_memory allocated values 17148ada 64 bit values allocated on lbm_memory on 32 bit version 4445e5a8 sketch of encoders for 64 bit values f636e64d suffix for the lisp size of int and uint are now i and u instead of i28 and u28 840723ca preparations and planning for 64bit types in both 32 and 64 bit versions. 13675dda 64bit up and running. but there are many TODOs 033bfd9a small fixes following bug hunt f5c984de fix bug in representation of important masks and constants d40b2437 Merge branch 'master' into dev64bit 06f9603f Merge branch 'master' into dev64bit 69950ba1 32 bit tests are ok 46b6fa28 Merge branch 'master' into dev64bit b9e2c993 work in progress 8dff9b4d work in progress a241aded work in progress 4738e0da update year in lbm_types ca469923 made it possible to run the same tests as on the 32 bit version on the 64 bit code 85acec30 small tweaks tp create_ctx, use correct type bf0286a7 Merge branch 'master' into dev64bit 824e1634 Merge branch 'master' into dev64bit 04a97f17 lots of testing needed ea862cce a bunch of changes in preparation for 64bit compatibility. git-subtree-dir: lispBM/lispBM git-subtree-split: 43ebce71a281f4a158b8d2dec3fc537c26766ec3
2022-03-25 07:47:05 -07:00
extern lbm_uint lbm_get_symbol_table_size(void);
#endif