alang.c 103 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826
  1. // Based on https://github.com/Kode/Kongruent by RobDangerous
  2. #include "alang.h"
  3. #include <assert.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <stddef.h>
  8. #include <stdbool.h>
  9. #include <stdarg.h>
  10. // #define STB_DS_IMPLEMENTATION
  11. #include "../../sources/libs/kong/libs/stb_ds.h"
  12. #ifdef WIN32
  13. #include <Windows.h>
  14. #endif
  15. #ifdef __android__
  16. #include <android/log.h>
  17. #endif
  18. #define static_array(type, name, max_size) \
  19. typedef struct name { \
  20. type values[max_size]; \
  21. size_t size; \
  22. size_t max; \
  23. } name;
  24. #define static_array_init(array) \
  25. array.size = 0; \
  26. array.max = sizeof(array.values) / sizeof(array.values[0])
  27. #define static_array_push(array, value) \
  28. if (array.size >= array.max) { \
  29. debug_context context = {0}; \
  30. error(context, "Array overflow"); \
  31. } \
  32. array.values[array.size++] = value;
  33. #define static_array_push_p(array, value) \
  34. if (array->size >= array->max) { \
  35. debug_context context = {0}; \
  36. error(context, "Array overflow"); \
  37. } \
  38. array->values[array->size++] = value;
  39. // based on https://valkey.io/blog/new-hash-table/
  40. struct meta {
  41. uint8_t c : 1;
  42. uint8_t presence : 7;
  43. uint8_t hashes[7];
  44. };
  45. struct container {
  46. int key;
  47. };
  48. struct bucket {
  49. struct meta meta;
  50. void *entries[7];
  51. };
  52. #define HASH_MAP_SIZE 256
  53. struct hash_map {
  54. struct bucket buckets[HASH_MAP_SIZE];
  55. };
  56. static inline uint64_t hash(int key) {
  57. uint64_t primary = key % HASH_MAP_SIZE;
  58. uint8_t secondary = key % HASH_MAP_SIZE;
  59. return primary | ((uint64_t)secondary << 56);
  60. }
  61. static inline struct bucket *allocate_bucket(void) {
  62. #ifdef _WIN32
  63. struct bucket *new_bucket = (struct bucket *)_aligned_malloc(sizeof(struct bucket), 64);
  64. #else
  65. struct bucket *new_bucket = (struct bucket *)aligned_alloc(64, sizeof(struct bucket));
  66. #endif
  67. assert(new_bucket != NULL);
  68. memset(new_bucket, 0, sizeof(struct bucket));
  69. // cache line check
  70. assert((uint64_t)new_bucket % 64 == 0);
  71. assert(sizeof(struct bucket) == 64);
  72. return new_bucket;
  73. }
  74. static inline struct hash_map *hash_map_create(void) {
  75. #ifdef _WIN32
  76. struct hash_map *map = _aligned_malloc(sizeof(struct hash_map), 64);
  77. #else
  78. struct hash_map *map = aligned_alloc(64, sizeof(struct hash_map));
  79. #endif
  80. assert(map != NULL);
  81. memset(map, 0, sizeof(struct hash_map));
  82. // cache line check
  83. assert((uint64_t)map % 64 == 0);
  84. return map;
  85. }
  86. static inline void hash_map_destroy(struct hash_map *map) {}
  87. static inline void hash_map_add_to_bucket(struct bucket *bucket, struct container *value, uint8_t secondary_hash) {
  88. for (uint32_t index = 0; index < 7; ++index) {
  89. if (((bucket->meta.presence >> index) & 1) == 0) {
  90. bucket->entries[index] = value;
  91. bucket->meta.hashes[index] = secondary_hash;
  92. bucket->meta.presence |= (1 << index);
  93. return;
  94. }
  95. }
  96. if (bucket->meta.c) {
  97. hash_map_add_to_bucket((struct bucket *)bucket->entries[6], value, secondary_hash);
  98. }
  99. else {
  100. bucket->meta.c = 1;
  101. struct container *previous = (struct container *)bucket->entries[6];
  102. uint8_t previous_secondary_hash = bucket->meta.hashes[6];
  103. struct bucket *new_bucket = allocate_bucket();
  104. bucket->entries[6] = new_bucket;
  105. hash_map_add_to_bucket(new_bucket, previous, previous_secondary_hash);
  106. hash_map_add_to_bucket(new_bucket, value, secondary_hash);
  107. }
  108. }
  109. static inline void hash_map_add(struct hash_map *map, struct container *value) {
  110. uint64_t hash_value = hash(value->key);
  111. uint64_t primary = hash_value & 0xffffffffffffffu;
  112. uint8_t secondary = (uint8_t)(hash_value << 56);
  113. hash_map_add_to_bucket(&map->buckets[primary], value, secondary);
  114. }
  115. static inline struct container *hash_map_get_from_bucket(struct bucket *bucket, int key, uint8_t secondary_hash) {
  116. for (uint32_t index = 0; index < 6; ++index) {
  117. if (((bucket->meta.presence >> index) & 1) != 0 && bucket->meta.hashes[index] == secondary_hash) {
  118. struct container *value = (struct container *)bucket->entries[index];
  119. if (value->key == key) {
  120. return value;
  121. }
  122. }
  123. }
  124. if (bucket->meta.c) {
  125. struct bucket *next_bucket = (struct bucket *)bucket->entries[6];
  126. return hash_map_get_from_bucket(next_bucket, key, secondary_hash);
  127. }
  128. else {
  129. if (((bucket->meta.presence >> 6) & 1) != 0 && bucket->meta.hashes[6] == secondary_hash) {
  130. struct container *value = (struct container *)bucket->entries[6];
  131. if (value->key == key) {
  132. return value;
  133. }
  134. }
  135. }
  136. return NULL;
  137. }
  138. static inline struct container *hash_map_get(struct hash_map *map, int key) {
  139. uint64_t hash_value = hash(key);
  140. uint64_t primary = hash_value & 0xffffffffffffffu;
  141. uint8_t secondary = (uint8_t)(hash_value << 56);
  142. struct bucket *bucket = &map->buckets[primary];
  143. return hash_map_get_from_bucket(bucket, key, secondary);
  144. }
  145. static inline void hash_map_iterate_in_bucket(struct bucket *bucket, void (*callback)(struct container *, void *data), void *data) {
  146. for (uint32_t index = 0; index < 6; ++index) {
  147. if (((bucket->meta.presence >> index) & 1) != 0) {
  148. struct container *value = (struct container *)bucket->entries[index];
  149. callback(value, data);
  150. }
  151. }
  152. if (bucket->meta.c) {
  153. struct bucket *next_bucket = (struct bucket *)bucket->entries[6];
  154. hash_map_iterate_in_bucket(next_bucket, callback, data);
  155. }
  156. else {
  157. if (((bucket->meta.presence >> 6) & 1) != 0) {
  158. struct container *value = (struct container *)bucket->entries[6];
  159. callback(value, data);
  160. }
  161. }
  162. }
  163. static inline void hash_map_iterate(struct hash_map *map, void (*callback)(struct container *, void *data), void *data) {
  164. for (uint32_t bucket_index = 0; bucket_index < HASH_MAP_SIZE; ++bucket_index) {
  165. struct bucket *bucket = &map->buckets[bucket_index];
  166. hash_map_iterate_in_bucket(bucket, callback, data);
  167. }
  168. }
  169. typedef struct debug_context {
  170. const char *filename;
  171. uint32_t column;
  172. uint32_t line;
  173. } debug_context;
  174. static void error(debug_context context, const char *message, ...);
  175. static void error_no_context(const char *message, ...);
  176. static void error_args(debug_context context, const char *message, va_list args);
  177. static void error_args_no_context(const char *message, va_list args);
  178. static void check_function(bool test, debug_context context, const char *message, ...);
  179. #define check(test, context, message, ...) \
  180. assert(test); \
  181. check_function(test, context, message, ##__VA_ARGS__)
  182. typedef enum { LOG_LEVEL_INFO, LOG_LEVEL_WARNING, LOG_LEVEL_ERROR } log_level_t;
  183. static void kong_log(log_level_t log_level, const char *format, ...);
  184. static void kong_log_args(log_level_t log_level, const char *format, va_list args);
  185. ////
  186. #define OP_SIZE(op, opmember) offsetof(opcode, opmember) + sizeof(op.opmember)
  187. #define NO_FUNCTION 0xFFFFFFFF
  188. #define NO_NAME 0
  189. #define NO_TYPE 0xFFFFFFFF
  190. #define MAX_MEMBERS 1024
  191. struct statement;
  192. typedef uint32_t global_id;
  193. typedef struct global_array {
  194. global_id globals[256];
  195. bool readable[256];
  196. bool writable[256];
  197. size_t size;
  198. } global_array;
  199. struct expression;
  200. typedef struct expressions {
  201. struct expression *e[256];
  202. size_t size;
  203. } expressions;
  204. typedef struct expression {
  205. enum {
  206. EXPRESSION_BINARY,
  207. EXPRESSION_UNARY,
  208. EXPRESSION_BOOLEAN,
  209. EXPRESSION_FLOAT,
  210. EXPRESSION_INT,
  211. // EXPRESSION_STRING,
  212. EXPRESSION_VARIABLE,
  213. EXPRESSION_GROUPING,
  214. EXPRESSION_CALL,
  215. EXPRESSION_MEMBER,
  216. EXPRESSION_ELEMENT
  217. } kind;
  218. type_ref type;
  219. union {
  220. struct {
  221. struct expression *left;
  222. operatorr op;
  223. struct expression *right;
  224. } binary;
  225. struct {
  226. operatorr op;
  227. struct expression *right;
  228. } unary;
  229. bool boolean;
  230. double number;
  231. // char string[MAX_IDENTIFIER_SIZE];
  232. name_id variable;
  233. uint32_t index;
  234. struct expression *grouping;
  235. struct {
  236. name_id func_name;
  237. expressions parameters;
  238. } call;
  239. struct {
  240. struct expression *of;
  241. name_id member_name;
  242. } member;
  243. struct {
  244. struct expression *of;
  245. struct expression *element_index;
  246. } element;
  247. };
  248. } expression;
  249. struct statement;
  250. typedef struct statements {
  251. struct statement *s[256];
  252. size_t size;
  253. } statements;
  254. typedef struct local_variable {
  255. name_id name;
  256. type_ref type;
  257. uint64_t variable_id;
  258. } local_variable;
  259. typedef struct local_variables {
  260. local_variable v[256];
  261. size_t size;
  262. } local_variables;
  263. typedef struct block {
  264. struct block *parent;
  265. local_variables vars;
  266. statements statements;
  267. } block;
  268. typedef struct statement {
  269. enum {
  270. STATEMENT_EXPRESSION,
  271. STATEMENT_RETURN_EXPRESSION,
  272. STATEMENT_IF,
  273. STATEMENT_WHILE,
  274. STATEMENT_DO_WHILE,
  275. STATEMENT_BLOCK,
  276. STATEMENT_LOCAL_VARIABLE
  277. } kind;
  278. union {
  279. expression *expression;
  280. struct {
  281. expression *test;
  282. struct statement *if_block;
  283. expression *else_tests[64];
  284. struct statement *else_blocks[64];
  285. uint16_t else_size;
  286. } iffy;
  287. struct {
  288. expression *test;
  289. struct statement *while_block;
  290. } whiley;
  291. block block;
  292. struct {
  293. local_variable var;
  294. expression *init;
  295. } local_variable;
  296. };
  297. } statement;
  298. typedef struct definition {
  299. enum {
  300. DEFINITION_FUNCTION,
  301. DEFINITION_STRUCT,
  302. DEFINITION_CONST_CUSTOM,
  303. DEFINITION_CONST_BASIC
  304. } kind;
  305. union {
  306. function_id function;
  307. global_id global;
  308. type_id type;
  309. };
  310. } definition;
  311. typedef struct member {
  312. name_id name;
  313. type_ref type;
  314. token value;
  315. } member;
  316. typedef struct members {
  317. member m[MAX_MEMBERS];
  318. size_t size;
  319. } members;
  320. typedef struct type {
  321. attribute_list attributes;
  322. name_id name;
  323. bool built_in;
  324. members members;
  325. type_id base;
  326. uint32_t array_size;
  327. } type;
  328. static void allocate_globals(void);
  329. void _compile_function_block(opcodes *code, struct statement *block);
  330. static variable allocate_variable(type_ref type, variable_kind kind);
  331. void _functions_init(void);
  332. static function_id add_function(name_id name);
  333. static function_id find_function(name_id name);
  334. function *_get_function(function_id function);
  335. void _globals_init(void);
  336. static global_id add_global(type_id type, attribute_list attributes, name_id name);
  337. static global_id add_global_with_value(type_id type, attribute_list attributes, name_id name, global_value value);
  338. static global *find_global(name_id name);
  339. static global *get_global(global_id id);
  340. static void assign_global_var(global_id id, uint64_t var_index);
  341. void _names_init(void);
  342. static name_id add_name(char *name);
  343. char *_get_name(name_id index);
  344. void _parse(const char *filename, tokens *tokens);
  345. static token tokens_get(tokens *arr, size_t index);
  346. tokens _tokenize(const char *filename, const char *source);
  347. void _resolve_types(void);
  348. static void init_type_ref(type_ref *t, name_id name);
  349. void _types_init(void);
  350. static type_id add_type(name_id name);
  351. static type_id add_full_type(type *t);
  352. static type_id find_type_by_name(name_id name);
  353. static type_id find_type_by_ref(type_ref *t);
  354. static type *get_type(type_id t);
  355. static void kong_log(log_level_t level, const char *format, ...) {
  356. va_list args;
  357. va_start(args, format);
  358. kong_log_args(level, format, args);
  359. va_end(args);
  360. }
  361. static void kong_log_args(log_level_t level, const char *format, va_list args) {
  362. #ifdef WIN32
  363. {
  364. char buffer[4096];
  365. vsnprintf(buffer, 4090, format, args);
  366. strcat(buffer, "\r\n");
  367. OutputDebugStringA(buffer);
  368. }
  369. #endif
  370. {
  371. char buffer[4096];
  372. vsnprintf(buffer, 4090, format, args);
  373. strcat(buffer, "\n");
  374. fprintf(level == LOG_LEVEL_INFO ? stdout : stderr, "%s", buffer);
  375. }
  376. }
  377. static void debug_break(void) {
  378. #ifndef NDEBUG
  379. #if defined(_MSC_VER)
  380. __debugbreak();
  381. #elif defined(__clang__)
  382. __builtin_debugtrap();
  383. #else
  384. #if defined(__aarch64__)
  385. __asm__ volatile(".inst 0xd4200000");
  386. #elif defined(__x86_64__)
  387. __asm__ volatile("int $0x03");
  388. #else
  389. kong_log(LOG_LEVEL_WARNING, "Oh no, debug_break is not implemented for the current compiler and CPU.");
  390. #endif
  391. #endif
  392. #endif
  393. }
  394. static void error_args(debug_context context, const char *message, va_list args) {
  395. char buffer[4096];
  396. if (context.filename != NULL) {
  397. sprintf(buffer, "In column %i at line %i in %s: ", context.column + 1, context.line + 1, context.filename);
  398. }
  399. else {
  400. sprintf(buffer, "In column %i at line %i: ", context.column + 1, context.line + 1);
  401. }
  402. strcat(buffer, message);
  403. kong_log_args(LOG_LEVEL_ERROR, buffer, args);
  404. debug_break();
  405. exit(1);
  406. }
  407. static void error_args_no_context(const char *message, va_list args) {
  408. kong_log_args(LOG_LEVEL_ERROR, message, args);
  409. exit(1);
  410. }
  411. static void error(debug_context context, const char *message, ...) {
  412. va_list args;
  413. va_start(args, message);
  414. error_args(context, message, args);
  415. va_end(args);
  416. }
  417. static void error_no_context(const char *message, ...) {
  418. va_list args;
  419. va_start(args, message);
  420. error_args_no_context(message, args);
  421. va_end(args);
  422. }
  423. static void check_function(bool test, debug_context context, const char *message, ...) {
  424. if (!test) {
  425. va_list args;
  426. va_start(args, message);
  427. error_args(context, message, args);
  428. va_end(args);
  429. }
  430. }
  431. ////
  432. typedef enum modifier {
  433. MODIFIER_IN,
  434. // Out,
  435. } modifier_t;
  436. typedef enum mode {
  437. MODE_SELECT,
  438. MODE_NUMBER,
  439. // MODE_STRING,
  440. MODE_OPERATOR,
  441. MODE_IDENTIFIER,
  442. MODE_LINE_COMMENT,
  443. MODE_COMMENT
  444. } mode;
  445. typedef struct modifiers {
  446. modifier_t m[16];
  447. size_t size;
  448. } modifiers_t;
  449. typedef struct tokenizer_state {
  450. const char *iterator;
  451. char next;
  452. char next_next;
  453. int line, column;
  454. bool line_end;
  455. } tokenizer_state;
  456. typedef struct block_ids {
  457. uint64_t start;
  458. uint64_t end;
  459. } block_ids;
  460. typedef struct state {
  461. tokens *tokens;
  462. size_t index;
  463. debug_context context;
  464. } state_t;
  465. typedef struct tokenizer_buffer {
  466. char *buf;
  467. size_t current_size;
  468. size_t max_size;
  469. int column, line;
  470. } tokenizer_buffer;
  471. typedef struct prefix {
  472. char str[5];
  473. size_t size;
  474. } prefix;
  475. struct {
  476. char *key;
  477. name_id value;
  478. } *names_hash = NULL;
  479. allocated_global __allocated_globals[1024];
  480. size_t __allocated_globals_size = 0;
  481. static const char all_names[1024 * 1024];
  482. static uint64_t next_variable_id = 1;
  483. static variable all_variables[1024 * 1024];
  484. static function *functions = NULL;
  485. static function_id functions_size = 128;
  486. static function_id next_function_index = 0;
  487. static global globals[1024];
  488. static global_id globals_size = 0;
  489. static char *names = NULL;
  490. static size_t names_size = 1024 * 1024;
  491. static name_id names_index = 1;
  492. static statement statements_buffer[8192];
  493. static int statement_index = 0;
  494. static expression experessions_buffer[8192];
  495. static int expression_index = 0;
  496. static type *types = NULL;
  497. static type_id types_size = 1024;
  498. static type_id next_type_index = 0;
  499. type_id _void_id;
  500. type_id _float_id;
  501. type_id _int_id;
  502. type_id _uint_id;
  503. type_id _bool_id;
  504. static type_id function_type_id;
  505. static definition parse_definition(state_t *state);
  506. static statement *parse_statement(state_t *state, block *parent_block);
  507. static expression *parse_expression(state_t *state);
  508. static definition parse_struct(state_t *state);
  509. static definition parse_function(state_t *state);
  510. static definition parse_const(state_t *state, attribute_list attributes);
  511. static void resolve_types_in_expression(statement *parent, expression *e);
  512. static allocated_global find_allocated_global(name_id name) {
  513. for (size_t i = 0; i < __allocated_globals_size; ++i) {
  514. if (name == __allocated_globals[i].g->name) {
  515. return __allocated_globals[i];
  516. }
  517. }
  518. allocated_global a;
  519. a.g = NULL;
  520. a.variable_id = 0;
  521. return a;
  522. }
  523. static variable find_local_var(block *b, name_id name) {
  524. if (b == NULL) {
  525. variable var;
  526. var.index = 0;
  527. init_type_ref(&var.type, NO_NAME);
  528. return var;
  529. }
  530. for (size_t i = 0; i < b->vars.size; ++i) {
  531. if (b->vars.v[i].name == name) {
  532. debug_context context = {0};
  533. check(b->vars.v[i].type.type != NO_TYPE, context, "Local variable does not have a type");
  534. variable var;
  535. var.index = b->vars.v[i].variable_id;
  536. var.type = b->vars.v[i].type;
  537. var.kind = VARIABLE_LOCAL;
  538. return var;
  539. }
  540. }
  541. return find_local_var(b->parent, name);
  542. }
  543. static variable find_variable(block *parent, name_id name) {
  544. variable local_var = find_local_var(parent, name);
  545. if (local_var.index == 0) {
  546. allocated_global global = find_allocated_global(name);
  547. if (global.g->type != NO_TYPE && global.variable_id != 0) {
  548. variable v;
  549. init_type_ref(&v.type, NO_NAME);
  550. v.type.type = global.g->type;
  551. v.index = global.variable_id;
  552. v.kind = VARIABLE_GLOBAL;
  553. return v;
  554. }
  555. else {
  556. debug_context context = {0};
  557. error(context, "Variable %s not found", _get_name(name));
  558. variable v;
  559. v.index = 0;
  560. return v;
  561. }
  562. }
  563. else {
  564. return local_var;
  565. }
  566. }
  567. static variable allocate_variable(type_ref type, variable_kind kind) {
  568. variable v;
  569. v.index = next_variable_id;
  570. v.type = type;
  571. v.kind = kind;
  572. all_variables[v.index] = v;
  573. ++next_variable_id;
  574. return v;
  575. }
  576. static opcode *emit_op(opcodes *code, opcode *o) {
  577. assert(code->size + o->size < OPCODES_SIZE);
  578. uint8_t *location = &code->o[code->size];
  579. memcpy(&code->o[code->size], o, o->size);
  580. code->size += o->size;
  581. return (opcode *)location;
  582. }
  583. static variable emit_expression(opcodes *code, block *parent, expression *e) {
  584. switch (e->kind) {
  585. case EXPRESSION_BINARY: {
  586. expression *left = e->binary.left;
  587. expression *right = e->binary.right;
  588. debug_context context = {0};
  589. switch (e->binary.op) {
  590. case OPERATOR_EQUALS:
  591. case OPERATOR_NOT_EQUALS:
  592. case OPERATOR_GREATER:
  593. case OPERATOR_GREATER_EQUAL:
  594. case OPERATOR_LESS:
  595. case OPERATOR_LESS_EQUAL:
  596. case OPERATOR_AND:
  597. case OPERATOR_OR: {
  598. variable right_var = emit_expression(code, parent, right);
  599. variable left_var = emit_expression(code, parent, left);
  600. type_ref t;
  601. init_type_ref(&t, NO_NAME);
  602. t.type = _bool_id;
  603. variable result_var = allocate_variable(t, VARIABLE_INTERNAL);
  604. opcode o;
  605. switch (e->binary.op) {
  606. case OPERATOR_EQUALS:
  607. o.type = OPCODE_EQUALS;
  608. break;
  609. case OPERATOR_NOT_EQUALS:
  610. o.type = OPCODE_NOT_EQUALS;
  611. break;
  612. case OPERATOR_GREATER:
  613. o.type = OPCODE_GREATER;
  614. break;
  615. case OPERATOR_GREATER_EQUAL:
  616. o.type = OPCODE_GREATER_EQUAL;
  617. break;
  618. case OPERATOR_LESS:
  619. o.type = OPCODE_LESS;
  620. break;
  621. case OPERATOR_LESS_EQUAL:
  622. o.type = OPCODE_LESS_EQUAL;
  623. break;
  624. case OPERATOR_AND:
  625. o.type = OPCODE_AND;
  626. break;
  627. case OPERATOR_OR:
  628. o.type = OPCODE_OR;
  629. break;
  630. default: {
  631. error(context, "Unexpected operator");
  632. }
  633. }
  634. o.size = OP_SIZE(o, op_binary);
  635. o.op_binary.right = right_var;
  636. o.op_binary.left = left_var;
  637. o.op_binary.result = result_var;
  638. emit_op(code, &o);
  639. return result_var;
  640. }
  641. case OPERATOR_MINUS:
  642. case OPERATOR_PLUS:
  643. case OPERATOR_DIVIDE:
  644. case OPERATOR_MULTIPLY:
  645. case OPERATOR_MOD:
  646. case OPERATOR_BITWISE_XOR:
  647. case OPERATOR_BITWISE_AND:
  648. case OPERATOR_BITWISE_OR:
  649. case OPERATOR_LEFT_SHIFT:
  650. case OPERATOR_RIGHT_SHIFT: {
  651. variable right_var = emit_expression(code, parent, right);
  652. variable left_var = emit_expression(code, parent, left);
  653. variable result_var = allocate_variable(e->type, VARIABLE_INTERNAL);
  654. opcode o;
  655. switch (e->binary.op) {
  656. case OPERATOR_MINUS:
  657. o.type = OPCODE_SUB;
  658. break;
  659. case OPERATOR_PLUS:
  660. o.type = OPCODE_ADD;
  661. break;
  662. case OPERATOR_DIVIDE:
  663. o.type = OPCODE_DIVIDE;
  664. break;
  665. case OPERATOR_MULTIPLY:
  666. o.type = OPCODE_MULTIPLY;
  667. break;
  668. case OPERATOR_MOD:
  669. o.type = OPCODE_MOD;
  670. break;
  671. case OPERATOR_BITWISE_XOR:
  672. o.type = OPCODE_BITWISE_XOR;
  673. break;
  674. case OPERATOR_BITWISE_AND:
  675. o.type = OPCODE_BITWISE_AND;
  676. break;
  677. case OPERATOR_BITWISE_OR:
  678. o.type = OPCODE_BITWISE_OR;
  679. break;
  680. case OPERATOR_LEFT_SHIFT:
  681. o.type = OPCODE_LEFT_SHIFT;
  682. break;
  683. case OPERATOR_RIGHT_SHIFT:
  684. o.type = OPCODE_RIGHT_SHIFT;
  685. break;
  686. default: {
  687. error(context, "Unexpected operator");
  688. }
  689. }
  690. o.size = OP_SIZE(o, op_binary);
  691. o.op_binary.right = right_var;
  692. o.op_binary.left = left_var;
  693. o.op_binary.result = result_var;
  694. emit_op(code, &o);
  695. return result_var;
  696. }
  697. case OPERATOR_NOT: {
  698. error(context, "! is not a binary operator");
  699. }
  700. case OPERATOR_ASSIGN:
  701. case OPERATOR_MINUS_ASSIGN:
  702. case OPERATOR_PLUS_ASSIGN:
  703. case OPERATOR_DIVIDE_ASSIGN:
  704. case OPERATOR_MULTIPLY_ASSIGN: {
  705. variable v = emit_expression(code, parent, right);
  706. switch (left->kind) {
  707. case EXPRESSION_VARIABLE: {
  708. opcode o;
  709. switch (e->binary.op) {
  710. case OPERATOR_ASSIGN:
  711. o.type = OPCODE_STORE_VARIABLE;
  712. break;
  713. case OPERATOR_MINUS_ASSIGN:
  714. o.type = OPCODE_SUB_AND_STORE_VARIABLE;
  715. break;
  716. case OPERATOR_PLUS_ASSIGN:
  717. o.type = OPCODE_ADD_AND_STORE_VARIABLE;
  718. break;
  719. case OPERATOR_DIVIDE_ASSIGN:
  720. o.type = OPCODE_DIVIDE_AND_STORE_VARIABLE;
  721. break;
  722. case OPERATOR_MULTIPLY_ASSIGN:
  723. o.type = OPCODE_MULTIPLY_AND_STORE_VARIABLE;
  724. break;
  725. default: {
  726. error(context, "Unexpected operator");
  727. }
  728. }
  729. o.size = OP_SIZE(o, op_store_var);
  730. o.op_store_var.from = v;
  731. o.op_store_var.to = find_variable(parent, left->variable);
  732. emit_op(code, &o);
  733. break;
  734. }
  735. case EXPRESSION_ELEMENT:
  736. case EXPRESSION_MEMBER: {
  737. opcode o;
  738. switch (e->binary.op) {
  739. case OPERATOR_ASSIGN:
  740. o.type = OPCODE_STORE_ACCESS_LIST;
  741. break;
  742. case OPERATOR_MINUS_ASSIGN:
  743. o.type = OPCODE_SUB_AND_STORE_ACCESS_LIST;
  744. break;
  745. case OPERATOR_PLUS_ASSIGN:
  746. o.type = OPCODE_ADD_AND_STORE_ACCESS_LIST;
  747. break;
  748. case OPERATOR_DIVIDE_ASSIGN:
  749. o.type = OPCODE_DIVIDE_AND_STORE_ACCESS_LIST;
  750. break;
  751. case OPERATOR_MULTIPLY_ASSIGN:
  752. o.type = OPCODE_MULTIPLY_AND_STORE_ACCESS_LIST;
  753. break;
  754. default: {
  755. error(context, "Unexpected operator");
  756. }
  757. }
  758. o.size = OP_SIZE(o, op_store_access_list);
  759. o.op_store_access_list.from = v;
  760. expression *of = left;
  761. access access_list[64];
  762. uint32_t access_list_size = 0;
  763. while (of->kind == EXPRESSION_ELEMENT || of->kind == EXPRESSION_MEMBER) {
  764. access *a = &access_list[access_list_size];
  765. a->type = of->type.type;
  766. switch (of->kind) {
  767. case EXPRESSION_ELEMENT:
  768. a->kind = ACCESS_ELEMENT;
  769. a->access_element.index = emit_expression(code, parent, of->element.element_index);
  770. of = of->element.of;
  771. break;
  772. case EXPRESSION_MEMBER:
  773. a->kind = ACCESS_MEMBER;
  774. a->access_member.name = of->member.member_name;
  775. of = of->member.of;
  776. break;
  777. default:
  778. assert(false);
  779. break;
  780. }
  781. access_list_size += 1;
  782. }
  783. o.op_store_access_list.access_list_size = access_list_size;
  784. for (uint32_t access_index = 0; access_index < access_list_size; ++access_index) {
  785. o.op_store_access_list.access_list[access_list_size - access_index - 1] = access_list[access_index];
  786. }
  787. o.op_store_access_list.to = emit_expression(code, parent, of);
  788. emit_op(code, &o);
  789. break;
  790. }
  791. default: {
  792. debug_context context = {0};
  793. error(context, "Expected a variable or an access");
  794. }
  795. }
  796. return v;
  797. }
  798. }
  799. break;
  800. }
  801. case EXPRESSION_UNARY: {
  802. debug_context context = {0};
  803. switch (e->unary.op) {
  804. case OPERATOR_EQUALS:
  805. error(context, "not implemented");
  806. case OPERATOR_NOT_EQUALS:
  807. error(context, "not implemented");
  808. case OPERATOR_GREATER:
  809. error(context, "not implemented");
  810. case OPERATOR_GREATER_EQUAL:
  811. error(context, "not implemented");
  812. case OPERATOR_LESS:
  813. error(context, "not implemented");
  814. case OPERATOR_LESS_EQUAL:
  815. error(context, "not implemented");
  816. case OPERATOR_MINUS: {
  817. variable v = emit_expression(code, parent, e->unary.right);
  818. opcode o;
  819. o.type = OPCODE_NEGATE;
  820. o.size = OP_SIZE(o, op_negate);
  821. o.op_negate.from = v;
  822. o.op_negate.to = allocate_variable(v.type, VARIABLE_INTERNAL);
  823. emit_op(code, &o);
  824. return o.op_negate.to;
  825. }
  826. case OPERATOR_PLUS:
  827. error(context, "not implemented");
  828. case OPERATOR_DIVIDE:
  829. error(context, "not implemented");
  830. case OPERATOR_MULTIPLY:
  831. error(context, "not implemented");
  832. case OPERATOR_NOT: {
  833. variable v = emit_expression(code, parent, e->unary.right);
  834. opcode o;
  835. o.type = OPCODE_NOT;
  836. o.size = OP_SIZE(o, op_not);
  837. o.op_not.from = v;
  838. o.op_not.to = allocate_variable(v.type, VARIABLE_INTERNAL);
  839. emit_op(code, &o);
  840. return o.op_not.to;
  841. }
  842. case OPERATOR_OR:
  843. error(context, "not implemented");
  844. case OPERATOR_BITWISE_XOR:
  845. error(context, "not implemented");
  846. case OPERATOR_BITWISE_AND:
  847. error(context, "not implemented");
  848. case OPERATOR_BITWISE_OR:
  849. error(context, "not implemented");
  850. case OPERATOR_LEFT_SHIFT:
  851. error(context, "not implemented");
  852. case OPERATOR_RIGHT_SHIFT:
  853. error(context, "not implemented");
  854. case OPERATOR_AND:
  855. error(context, "not implemented");
  856. case OPERATOR_MOD:
  857. error(context, "not implemented");
  858. case OPERATOR_ASSIGN:
  859. error(context, "not implemented");
  860. case OPERATOR_PLUS_ASSIGN:
  861. case OPERATOR_MINUS_ASSIGN:
  862. case OPERATOR_MULTIPLY_ASSIGN:
  863. case OPERATOR_DIVIDE_ASSIGN:
  864. error(context, "not implemented");
  865. }
  866. }
  867. case EXPRESSION_BOOLEAN: {
  868. type_ref t;
  869. init_type_ref(&t, NO_NAME);
  870. t.type = _float_id;
  871. variable v = allocate_variable(t, VARIABLE_INTERNAL);
  872. opcode o;
  873. o.type = OPCODE_LOAD_BOOL_CONSTANT;
  874. o.size = OP_SIZE(o, op_load_bool_constant);
  875. o.op_load_bool_constant.boolean = e->boolean;
  876. o.op_load_bool_constant.to = v;
  877. emit_op(code, &o);
  878. return v;
  879. }
  880. case EXPRESSION_FLOAT: {
  881. type_ref t;
  882. init_type_ref(&t, NO_NAME);
  883. t.type = _float_id;
  884. variable v = allocate_variable(t, VARIABLE_INTERNAL);
  885. opcode o;
  886. o.type = OPCODE_LOAD_FLOAT_CONSTANT;
  887. o.size = OP_SIZE(o, op_load_float_constant);
  888. o.op_load_float_constant.number = (float)e->number;
  889. o.op_load_float_constant.to = v;
  890. emit_op(code, &o);
  891. return v;
  892. }
  893. case EXPRESSION_INT: {
  894. type_ref t;
  895. init_type_ref(&t, NO_NAME);
  896. t.type = _int_id;
  897. variable v = allocate_variable(t, VARIABLE_INTERNAL);
  898. opcode o;
  899. o.type = OPCODE_LOAD_INT_CONSTANT;
  900. o.size = OP_SIZE(o, op_load_float_constant);
  901. o.op_load_int_constant.number = (int)e->number;
  902. o.op_load_int_constant.to = v;
  903. emit_op(code, &o);
  904. return v;
  905. }
  906. // case EXPRESSION_STRING:
  907. // error("not implemented", 0, 0);
  908. case EXPRESSION_VARIABLE: {
  909. return find_variable(parent, e->variable);
  910. }
  911. case EXPRESSION_GROUPING: {
  912. return emit_expression(code, parent, e->grouping);
  913. }
  914. case EXPRESSION_CALL: {
  915. type_ref t;
  916. init_type_ref(&t, NO_NAME);
  917. t.type = e->type.type;
  918. variable v = allocate_variable(t, VARIABLE_INTERNAL);
  919. opcode o;
  920. o.type = OPCODE_CALL;
  921. o.size = OP_SIZE(o, op_call);
  922. o.op_call.func = e->call.func_name;
  923. o.op_call.var = v;
  924. debug_context context = {0};
  925. check(e->call.parameters.size <= sizeof(o.op_call.parameters) / sizeof(variable), context, "Call parameters missized");
  926. for (size_t i = 0; i < e->call.parameters.size; ++i) {
  927. o.op_call.parameters[i] = emit_expression(code, parent, e->call.parameters.e[i]);
  928. }
  929. o.op_call.parameters_size = (uint8_t)e->call.parameters.size;
  930. emit_op(code, &o);
  931. return v;
  932. }
  933. case EXPRESSION_ELEMENT:
  934. case EXPRESSION_MEMBER: {
  935. opcode o;
  936. o.type = OPCODE_LOAD_ACCESS_LIST;
  937. o.size = OP_SIZE(o, op_load_access_list);
  938. variable v = allocate_variable(e->type, VARIABLE_INTERNAL);
  939. o.op_load_access_list.to = v;
  940. expression *of = e;
  941. access access_list[64];
  942. uint32_t access_list_size = 0;
  943. while (of->kind == EXPRESSION_ELEMENT || of->kind == EXPRESSION_MEMBER) {
  944. access *a = &access_list[access_list_size];
  945. a->type = of->type.type;
  946. switch (of->kind) {
  947. case EXPRESSION_ELEMENT:
  948. a->kind = ACCESS_ELEMENT;
  949. a->access_element.index = emit_expression(code, parent, of->element.element_index);
  950. of = of->element.of;
  951. break;
  952. case EXPRESSION_MEMBER:
  953. a->kind = ACCESS_MEMBER;
  954. a->access_member.name = of->member.member_name;
  955. of = of->member.of;
  956. break;
  957. default:
  958. assert(false);
  959. break;
  960. }
  961. access_list_size += 1;
  962. }
  963. o.op_load_access_list.access_list_size = access_list_size;
  964. for (uint32_t access_index = 0; access_index < access_list_size; ++access_index) {
  965. o.op_load_access_list.access_list[access_list_size - access_index - 1] = access_list[access_index];
  966. }
  967. o.op_load_access_list.from = emit_expression(code, parent, of);
  968. emit_op(code, &o);
  969. return v;
  970. }
  971. default: {
  972. debug_context context = {0};
  973. error(context, "not implemented");
  974. }
  975. }
  976. {
  977. debug_context context = {0};
  978. error(context, "Supposedly unreachable code reached");
  979. variable v;
  980. v.index = 0;
  981. return v;
  982. }
  983. }
  984. static block_ids emit_statement(opcodes *code, block *parent, statement *statement) {
  985. switch (statement->kind) {
  986. case STATEMENT_EXPRESSION:
  987. emit_expression(code, parent, statement->expression);
  988. break;
  989. case STATEMENT_RETURN_EXPRESSION: {
  990. opcode o;
  991. o.type = OPCODE_RETURN;
  992. variable v = emit_expression(code, parent, statement->expression);
  993. if (v.index == 0) {
  994. o.size = offsetof(opcode, op_return);
  995. }
  996. else {
  997. o.size = OP_SIZE(o, op_return);
  998. o.op_return.var = v;
  999. }
  1000. emit_op(code, &o);
  1001. break;
  1002. }
  1003. case STATEMENT_IF: {
  1004. struct previous_condition {
  1005. variable condition;
  1006. variable summed_condition;
  1007. };
  1008. struct previous_condition previous_conditions[64] = {0};
  1009. uint8_t previous_conditions_size = 0;
  1010. {
  1011. opcode o;
  1012. o.type = OPCODE_IF;
  1013. o.size = OP_SIZE(o, op_if);
  1014. variable initial_condition = emit_expression(code, parent, statement->iffy.test);
  1015. o.op_if.condition = initial_condition;
  1016. opcode *written_opcode = emit_op(code, &o);
  1017. previous_conditions[previous_conditions_size].condition = initial_condition;
  1018. previous_conditions_size += 1;
  1019. block_ids ids = emit_statement(code, parent, statement->iffy.if_block);
  1020. written_opcode->op_if.start_id = ids.start;
  1021. written_opcode->op_if.end_id = ids.end;
  1022. }
  1023. for (uint16_t i = 0; i < statement->iffy.else_size; ++i) {
  1024. variable current_condition;
  1025. {
  1026. opcode o;
  1027. o.type = OPCODE_NOT;
  1028. o.size = OP_SIZE(o, op_not);
  1029. o.op_not.from = previous_conditions[previous_conditions_size - 1].condition;
  1030. type_ref t;
  1031. init_type_ref(&t, NO_NAME);
  1032. t.type = _bool_id;
  1033. current_condition = allocate_variable(t, VARIABLE_INTERNAL);
  1034. o.op_not.to = current_condition;
  1035. emit_op(code, &o);
  1036. }
  1037. variable summed_condition;
  1038. if (previous_conditions_size == 1) {
  1039. summed_condition = previous_conditions[0].summed_condition = current_condition;
  1040. }
  1041. else {
  1042. opcode o;
  1043. o.type = OPCODE_AND;
  1044. o.size = OP_SIZE(o, op_binary);
  1045. o.op_binary.left = previous_conditions[previous_conditions_size - 2].summed_condition;
  1046. o.op_binary.right = current_condition;
  1047. type_ref t;
  1048. init_type_ref(&t, NO_NAME);
  1049. t.type = _bool_id;
  1050. summed_condition = allocate_variable(t, VARIABLE_INTERNAL);
  1051. o.op_binary.result = summed_condition;
  1052. emit_op(code, &o);
  1053. }
  1054. opcode o;
  1055. o.type = OPCODE_IF;
  1056. o.size = OP_SIZE(o, op_if);
  1057. if (statement->iffy.else_tests[i] != NULL) {
  1058. variable v = emit_expression(code, parent, statement->iffy.else_tests[i]);
  1059. variable else_test;
  1060. {
  1061. opcode o;
  1062. o.type = OPCODE_AND;
  1063. o.size = OP_SIZE(o, op_binary);
  1064. o.op_binary.left = summed_condition;
  1065. o.op_binary.right = v;
  1066. type_ref t;
  1067. init_type_ref(&t, NO_NAME);
  1068. t.type = _bool_id;
  1069. else_test = allocate_variable(t, VARIABLE_INTERNAL);
  1070. o.op_binary.result = else_test;
  1071. emit_op(code, &o);
  1072. }
  1073. o.op_if.condition = else_test;
  1074. previous_conditions[previous_conditions_size].condition = v;
  1075. previous_conditions_size += 1;
  1076. }
  1077. else {
  1078. o.op_if.condition = summed_condition;
  1079. }
  1080. {
  1081. opcode *written_opcode = emit_op(code, &o);
  1082. block_ids ids = emit_statement(code, parent, statement->iffy.else_blocks[i]);
  1083. written_opcode->op_if.start_id = ids.start;
  1084. written_opcode->op_if.end_id = ids.end;
  1085. }
  1086. }
  1087. break;
  1088. }
  1089. case STATEMENT_WHILE: {
  1090. uint64_t start_id = next_variable_id;
  1091. ++next_variable_id;
  1092. uint64_t continue_id = next_variable_id;
  1093. ++next_variable_id;
  1094. uint64_t end_id = next_variable_id;
  1095. ++next_variable_id;
  1096. {
  1097. opcode o;
  1098. o.type = OPCODE_WHILE_START;
  1099. o.op_while_start.start_id = start_id;
  1100. o.op_while_start.continue_id = continue_id;
  1101. o.op_while_start.end_id = end_id;
  1102. o.size = OP_SIZE(o, op_while_start);
  1103. emit_op(code, &o);
  1104. }
  1105. {
  1106. opcode o;
  1107. o.type = OPCODE_WHILE_CONDITION;
  1108. o.size = OP_SIZE(o, op_while);
  1109. variable v = emit_expression(code, parent, statement->whiley.test);
  1110. o.op_while.condition = v;
  1111. o.op_while.end_id = end_id;
  1112. emit_op(code, &o);
  1113. }
  1114. emit_statement(code, parent, statement->whiley.while_block);
  1115. {
  1116. opcode o;
  1117. o.type = OPCODE_WHILE_END;
  1118. o.op_while_end.start_id = start_id;
  1119. o.op_while_end.continue_id = continue_id;
  1120. o.op_while_end.end_id = end_id;
  1121. o.size = OP_SIZE(o, op_while_end);
  1122. emit_op(code, &o);
  1123. }
  1124. break;
  1125. }
  1126. case STATEMENT_DO_WHILE: {
  1127. uint64_t start_id = next_variable_id;
  1128. ++next_variable_id;
  1129. uint64_t continue_id = next_variable_id;
  1130. ++next_variable_id;
  1131. uint64_t end_id = next_variable_id;
  1132. ++next_variable_id;
  1133. {
  1134. opcode o;
  1135. o.type = OPCODE_WHILE_START;
  1136. o.op_while_start.start_id = start_id;
  1137. o.op_while_start.continue_id = continue_id;
  1138. o.op_while_start.end_id = end_id;
  1139. o.size = OP_SIZE(o, op_while_start);
  1140. emit_op(code, &o);
  1141. }
  1142. emit_statement(code, parent, statement->whiley.while_block);
  1143. {
  1144. opcode o;
  1145. o.type = OPCODE_WHILE_CONDITION;
  1146. o.size = OP_SIZE(o, op_while);
  1147. variable v = emit_expression(code, parent, statement->whiley.test);
  1148. o.op_while.condition = v;
  1149. o.op_while.end_id = end_id;
  1150. emit_op(code, &o);
  1151. }
  1152. {
  1153. opcode o;
  1154. o.type = OPCODE_WHILE_END;
  1155. o.op_while_end.start_id = start_id;
  1156. o.op_while_end.continue_id = continue_id;
  1157. o.op_while_end.end_id = end_id;
  1158. o.size = OP_SIZE(o, op_while_end);
  1159. emit_op(code, &o);
  1160. }
  1161. break;
  1162. }
  1163. case STATEMENT_BLOCK: {
  1164. for (size_t i = 0; i < statement->block.vars.size; ++i) {
  1165. variable var = allocate_variable(statement->block.vars.v[i].type, VARIABLE_LOCAL);
  1166. statement->block.vars.v[i].variable_id = var.index;
  1167. }
  1168. uint64_t start_block_id = next_variable_id;
  1169. ++next_variable_id;
  1170. uint64_t end_block_id = next_variable_id;
  1171. ++next_variable_id;
  1172. {
  1173. opcode o;
  1174. o.type = OPCODE_BLOCK_START;
  1175. o.op_block.id = start_block_id;
  1176. o.size = OP_SIZE(o, op_block);
  1177. emit_op(code, &o);
  1178. }
  1179. for (size_t i = 0; i < statement->block.statements.size; ++i) {
  1180. emit_statement(code, &statement->block, statement->block.statements.s[i]);
  1181. }
  1182. {
  1183. opcode o;
  1184. o.type = OPCODE_BLOCK_END;
  1185. o.op_block.id = end_block_id;
  1186. o.size = OP_SIZE(o, op_block);
  1187. emit_op(code, &o);
  1188. }
  1189. block_ids ids;
  1190. ids.start = start_block_id;
  1191. ids.end = end_block_id;
  1192. return ids;
  1193. }
  1194. case STATEMENT_LOCAL_VARIABLE: {
  1195. opcode o;
  1196. o.type = OPCODE_VAR;
  1197. o.size = OP_SIZE(o, op_var);
  1198. variable init_var = {0};
  1199. if (statement->local_variable.init != NULL) {
  1200. init_var = emit_expression(code, parent, statement->local_variable.init);
  1201. }
  1202. variable local_var = find_local_var(parent, statement->local_variable.var.name);
  1203. statement->local_variable.var.variable_id = local_var.index;
  1204. o.op_var.var.index = statement->local_variable.var.variable_id;
  1205. debug_context context = {0};
  1206. check(statement->local_variable.var.type.type != NO_TYPE, context, "Local var has no type");
  1207. o.op_var.var.type = statement->local_variable.var.type;
  1208. emit_op(code, &o);
  1209. if (statement->local_variable.init != NULL) {
  1210. opcode o;
  1211. o.type = OPCODE_STORE_VARIABLE;
  1212. o.size = OP_SIZE(o, op_store_var);
  1213. o.op_store_var.from = init_var;
  1214. o.op_store_var.to = local_var;
  1215. emit_op(code, &o);
  1216. }
  1217. break;
  1218. }
  1219. }
  1220. block_ids ids;
  1221. ids.start = 0;
  1222. ids.end = 0;
  1223. return ids;
  1224. }
  1225. static void allocate_globals(void) {
  1226. for (global_id i = 0; get_global(i) != NULL && get_global(i)->type != NO_TYPE; ++i) {
  1227. global *g = get_global(i);
  1228. type_ref t;
  1229. init_type_ref(&t, NO_NAME);
  1230. t.type = g->type;
  1231. variable v = allocate_variable(t, VARIABLE_GLOBAL);
  1232. __allocated_globals[__allocated_globals_size].g = g;
  1233. __allocated_globals[__allocated_globals_size].variable_id = v.index;
  1234. __allocated_globals_size += 1;
  1235. assign_global_var(i, v.index);
  1236. }
  1237. }
  1238. void _compile_function_block(opcodes *code, struct statement *block) {
  1239. if (block == NULL) {
  1240. // built-in
  1241. return;
  1242. }
  1243. if (block->kind != STATEMENT_BLOCK) {
  1244. debug_context context = {0};
  1245. error(context, "Expected a block");
  1246. }
  1247. for (size_t i = 0; i < block->block.vars.size; ++i) {
  1248. variable var = allocate_variable(block->block.vars.v[i].type, VARIABLE_LOCAL);
  1249. block->block.vars.v[i].variable_id = var.index;
  1250. }
  1251. for (size_t i = 0; i < block->block.statements.size; ++i) {
  1252. emit_statement(code, &block->block, block->block.statements.s[i]);
  1253. }
  1254. }
  1255. static void add_func_int(char *name) {
  1256. function_id func = add_function(add_name(name));
  1257. function *f = _get_function(func);
  1258. init_type_ref(&f->return_type, add_name("int"));
  1259. f->return_type.type = find_type_by_ref(&f->return_type);
  1260. f->parameters_size = 0;
  1261. f->block = NULL;
  1262. }
  1263. static void add_func_float(char *name) {
  1264. function_id func = add_function(add_name(name));
  1265. function *f = _get_function(func);
  1266. init_type_ref(&f->return_type, add_name("float"));
  1267. f->return_type.type = find_type_by_ref(&f->return_type);
  1268. f->parameters_size = 0;
  1269. f->block = NULL;
  1270. }
  1271. static void add_func_uint(char *name) {
  1272. function_id func = add_function(add_name(name));
  1273. function *f = _get_function(func);
  1274. init_type_ref(&f->return_type, add_name("uint"));
  1275. f->return_type.type = find_type_by_ref(&f->return_type);
  1276. f->parameters_size = 0;
  1277. f->block = NULL;
  1278. }
  1279. static void add_func_float_float(char *name) {
  1280. function_id func = add_function(add_name(name));
  1281. function *f = _get_function(func);
  1282. init_type_ref(&f->return_type, add_name("float"));
  1283. f->return_type.type = find_type_by_ref(&f->return_type);
  1284. f->parameter_names[0] = add_name("a");
  1285. init_type_ref(&f->parameter_types[0], add_name("float"));
  1286. f->parameter_types[0].type = find_type_by_ref(&f->parameter_types[0]);
  1287. f->parameters_size = 1;
  1288. f->block = NULL;
  1289. }
  1290. void _functions_init(void) {
  1291. function *new_functions = realloc(functions, functions_size * sizeof(function));
  1292. debug_context context = {0};
  1293. check(new_functions != NULL, context, "Could not allocate functions");
  1294. functions = new_functions;
  1295. next_function_index = 0;
  1296. {
  1297. function_id func = add_function(add_name("float"));
  1298. function *f = _get_function(func);
  1299. init_type_ref(&f->return_type, add_name("float"));
  1300. f->return_type.type = find_type_by_ref(&f->return_type);
  1301. f->parameter_names[0] = add_name("x");
  1302. init_type_ref(&f->parameter_types[0], add_name("float"));
  1303. f->parameter_types[0].type = find_type_by_ref(&f->parameter_types[0]);
  1304. f->parameters_size = 1;
  1305. f->block = NULL;
  1306. }
  1307. {
  1308. function_id func = add_function(add_name("int"));
  1309. function *f = _get_function(func);
  1310. init_type_ref(&f->return_type, add_name("int"));
  1311. f->return_type.type = find_type_by_ref(&f->return_type);
  1312. f->parameter_names[0] = add_name("x");
  1313. init_type_ref(&f->parameter_types[0], add_name("int"));
  1314. f->parameter_types[0].type = find_type_by_ref(&f->parameter_types[0]);
  1315. f->parameters_size = 1;
  1316. f->block = NULL;
  1317. }
  1318. {
  1319. function_id func = add_function(add_name("uint"));
  1320. function *f = _get_function(func);
  1321. init_type_ref(&f->return_type, add_name("uint"));
  1322. f->return_type.type = find_type_by_ref(&f->return_type);
  1323. f->parameter_names[0] = add_name("x");
  1324. init_type_ref(&f->parameter_types[0], add_name("uint"));
  1325. f->parameter_types[0].type = find_type_by_ref(&f->parameter_types[0]);
  1326. f->parameters_size = 1;
  1327. f->block = NULL;
  1328. }
  1329. {
  1330. function_id func = add_function(add_name("bool"));
  1331. function *f = _get_function(func);
  1332. init_type_ref(&f->return_type, add_name("bool"));
  1333. f->return_type.type = find_type_by_ref(&f->return_type);
  1334. f->parameter_names[0] = add_name("x");
  1335. init_type_ref(&f->parameter_types[0], add_name("bool"));
  1336. f->parameter_types[0].type = find_type_by_ref(&f->parameter_types[0]);
  1337. f->parameters_size = 1;
  1338. f->block = NULL;
  1339. }
  1340. add_func_float_float("print");
  1341. // add_func_float_float("sin");
  1342. // add_func_float_float("cos");
  1343. }
  1344. static void grow_functions_if_needed(uint64_t size) {
  1345. while (size >= functions_size) {
  1346. functions_size *= 2;
  1347. function *new_functions = realloc(functions, functions_size * sizeof(function));
  1348. debug_context context = {0};
  1349. check(new_functions != NULL, context, "Could not allocate functions");
  1350. functions = new_functions;
  1351. }
  1352. }
  1353. static function_id add_function(name_id name) {
  1354. grow_functions_if_needed(next_function_index + 1);
  1355. function_id f = next_function_index;
  1356. ++next_function_index;
  1357. functions[f].name = name;
  1358. functions[f].attributes.attributes_count = 0;
  1359. init_type_ref(&functions[f].return_type, NO_NAME);
  1360. functions[f].parameters_size = 0;
  1361. memset(functions[f].parameter_attributes, 0, sizeof(functions[f].parameter_attributes));
  1362. functions[f].block = NULL;
  1363. memset(functions[f].code.o, 0, sizeof(functions[f].code.o));
  1364. functions[f].code.size = 0;
  1365. return f;
  1366. }
  1367. function *_get_function(function_id function) {
  1368. if (function >= next_function_index) {
  1369. return NULL;
  1370. }
  1371. return &functions[function];
  1372. }
  1373. void _globals_init(void) {
  1374. global_value int_value;
  1375. int_value.kind = GLOBAL_VALUE_INT;
  1376. attribute_list attributes = {0};
  1377. // int_value.value.ints[0] = 0;
  1378. // add_global_with_value(_float_id, attributes, add_name("COMPARE_ALWAYS"), int_value);
  1379. // global_value uint_value;
  1380. // uint_value.kind = GLOBAL_VALUE_UINT;
  1381. // uint_value.value.uints[0] = 0;
  1382. // add_global_with_value(_uint_id, attributes, add_name("TEXTURE_FORMAT_R8_UNORM"), uint_value);
  1383. }
  1384. static global_id add_global(type_id type, attribute_list attributes, name_id name) {
  1385. uint32_t index = globals_size;
  1386. globals[index].name = name;
  1387. globals[index].type = type;
  1388. globals[index].var_index = 0;
  1389. globals[index].value.kind = GLOBAL_VALUE_NONE;
  1390. globals[index].attributes = attributes;
  1391. globals[index].usage = 0;
  1392. globals_size += 1;
  1393. return index;
  1394. }
  1395. static global_id add_global_with_value(type_id type, attribute_list attributes, name_id name, global_value value) {
  1396. uint32_t index = globals_size;
  1397. globals[index].name = name;
  1398. globals[index].type = type;
  1399. globals[index].var_index = 0;
  1400. globals[index].value = value;
  1401. globals[index].attributes = attributes;
  1402. globals[index].usage = 0;
  1403. globals_size += 1;
  1404. return index;
  1405. }
  1406. static global *find_global(name_id name) {
  1407. for (uint32_t i = 0; i < globals_size; ++i) {
  1408. if (globals[i].name == name) {
  1409. return &globals[i];
  1410. }
  1411. }
  1412. return NULL;
  1413. }
  1414. static global *get_global(global_id id) {
  1415. if (id >= globals_size) {
  1416. return NULL;
  1417. }
  1418. return &globals[id];
  1419. }
  1420. static void assign_global_var(global_id id, uint64_t var_index) {
  1421. debug_context context = {0};
  1422. check(id < globals_size, context, "Encountered a global with a weird id");
  1423. globals[id].var_index = var_index;
  1424. }
  1425. void _names_init(void) {
  1426. char *new_names = realloc(names, names_size);
  1427. debug_context context = {0};
  1428. check(new_names != NULL, context, "Could not allocate names");
  1429. names = new_names;
  1430. names[0] = 0; // make NO_NAME a proper string
  1431. sh_new_arena(names_hash); // TODO: Get rid of this by using indices internally in the hash-map so it can survive grow_names_if_needed
  1432. }
  1433. static void grow_names_if_needed(size_t size) {
  1434. while (size >= names_size) {
  1435. names_size *= 2;
  1436. char *new_names = realloc(names, names_size);
  1437. debug_context context = {0};
  1438. check(new_names != NULL, context, "Could not allocate names");
  1439. names = new_names;
  1440. }
  1441. }
  1442. static name_id add_name(char *name) {
  1443. ptrdiff_t old_id_index = shgeti(names_hash, name);
  1444. if (old_id_index >= 0) {
  1445. return names_hash[old_id_index].value;
  1446. }
  1447. size_t length = strlen(name);
  1448. grow_names_if_needed(names_index + length + 1);
  1449. name_id id = names_index;
  1450. memcpy(&names[id], name, length);
  1451. names[id + length] = 0;
  1452. names_index += length + 1;
  1453. shput(names_hash, &names[id], id);
  1454. return id;
  1455. }
  1456. char *_get_name(name_id id) {
  1457. debug_context context = {0};
  1458. check(id < names_index, context, "Encountered a weird name id");
  1459. return &names[id];
  1460. }
  1461. static statement *statement_allocate(void) {
  1462. ////
  1463. // statement *s = (statement *)malloc(sizeof(statement));
  1464. statement *s = &statements_buffer[statement_index];
  1465. statement_index++;
  1466. ////
  1467. debug_context context = {0};
  1468. check(s != NULL, context, "Could not allocate statement");
  1469. return s;
  1470. }
  1471. // static void statement_free(statement *statement) {
  1472. // free(statement);
  1473. // }
  1474. static void statements_init(statements *statements) {
  1475. statements->size = 0;
  1476. }
  1477. static void statements_add(statements *statements, statement *statement) {
  1478. statements->s[statements->size] = statement;
  1479. statements->size += 1;
  1480. }
  1481. static expression *expression_allocate(void) {
  1482. ////
  1483. //expression *e = (expression *)malloc(sizeof(expression));
  1484. expression *e = &experessions_buffer[expression_index];
  1485. expression_index++;
  1486. ////
  1487. debug_context context = {0};
  1488. check(e != NULL, context, "Could not allocate expression");
  1489. init_type_ref(&e->type, NO_NAME);
  1490. return e;
  1491. }
  1492. // static void expression_free(expression *expression) {
  1493. // free(expression);
  1494. // }
  1495. static token current(state_t *state) {
  1496. token token = tokens_get(state->tokens, state->index);
  1497. return token;
  1498. }
  1499. static void update_debug_context(state_t *state) {
  1500. state->context.column = current(state).column;
  1501. state->context.line = current(state).line;
  1502. }
  1503. static void advance_state(state_t *state) {
  1504. state->index += 1;
  1505. update_debug_context(state);
  1506. }
  1507. static void match_token(state_t *state, int token, const char *error_message) {
  1508. int current_token = current(state).kind;
  1509. if (current_token != token) {
  1510. error(state->context, error_message);
  1511. }
  1512. }
  1513. static void match_token_identifier(state_t *state) {
  1514. if (current(state).kind != TOKEN_IDENTIFIER) {
  1515. error(state->context, "Expected an identifier");
  1516. }
  1517. }
  1518. void _parse(const char *filename, tokens *tokens) {
  1519. state_t state = {0};
  1520. state.context.filename = filename;
  1521. state.tokens = tokens;
  1522. state.index = 0;
  1523. for (;;) {
  1524. token token = current(&state);
  1525. if (token.kind == TOKEN_NONE) {
  1526. return;
  1527. }
  1528. else {
  1529. parse_definition(&state);
  1530. }
  1531. }
  1532. }
  1533. static statement *parse_block(state_t *state, block *parent_block) {
  1534. match_token(state, TOKEN_LEFT_CURLY, "Expected an opening curly bracket");
  1535. advance_state(state);
  1536. statements statements;
  1537. statements_init(&statements);
  1538. statement *new_block = statement_allocate();
  1539. new_block->kind = STATEMENT_BLOCK;
  1540. new_block->block.parent = parent_block;
  1541. new_block->block.vars.size = 0;
  1542. for (;;) {
  1543. switch (current(state).kind) {
  1544. case TOKEN_RIGHT_CURLY: {
  1545. advance_state(state);
  1546. new_block->block.statements = statements;
  1547. return new_block;
  1548. }
  1549. case TOKEN_NONE: {
  1550. update_debug_context(state);
  1551. error(state->context, "File ended before a block ended");
  1552. return NULL;
  1553. }
  1554. default:
  1555. statements_add(&statements, parse_statement(state, &new_block->block));
  1556. break;
  1557. }
  1558. }
  1559. }
  1560. // static void modifiers_init(modifiers_t *modifiers) {
  1561. // modifiers->size = 0;
  1562. // }
  1563. // static void modifiers_add(modifiers_t *modifiers, modifier_t modifier) {
  1564. // modifiers->m[modifiers->size] = modifier;
  1565. // modifiers->size += 1;
  1566. // }
  1567. static definition parse_definition(state_t *state) {
  1568. attribute_list attributes = {0};
  1569. switch (current(state).kind) {
  1570. case TOKEN_STRUCT: {
  1571. definition structy = parse_struct(state);
  1572. get_type(structy.type)->attributes = attributes;
  1573. return structy;
  1574. }
  1575. case TOKEN_FUNCTION: {
  1576. definition d = parse_function(state);
  1577. function *f = _get_function(d.function);
  1578. f->attributes = attributes;
  1579. return d;
  1580. }
  1581. case TOKEN_CONST: {
  1582. definition d = parse_const(state, attributes);
  1583. return d;
  1584. }
  1585. default: {
  1586. update_debug_context(state);
  1587. error(state->context, "Expected a struct, a function or a const");
  1588. definition d = {0};
  1589. return d;
  1590. }
  1591. }
  1592. }
  1593. static type_ref parse_type_ref(state_t *state) {
  1594. match_token(state, TOKEN_IDENTIFIER, "Expected an identifier");
  1595. token type_name = current(state);
  1596. advance_state(state);
  1597. uint32_t array_size = 0;
  1598. if (current(state).kind == TOKEN_LEFT_SQUARE) {
  1599. advance_state(state);
  1600. if (current(state).kind == TOKEN_INT) {
  1601. array_size = (uint32_t)current(state).number;
  1602. if (array_size == 0) {
  1603. error(state->context, "Array size of 0 is not allowed");
  1604. }
  1605. advance_state(state);
  1606. }
  1607. else {
  1608. array_size = UINT32_MAX;
  1609. }
  1610. match_token(state, TOKEN_RIGHT_SQUARE, "Expected a closing square bracket");
  1611. advance_state(state);
  1612. }
  1613. type_ref t;
  1614. init_type_ref(&t, type_name.identifier);
  1615. t.unresolved.array_size = array_size;
  1616. return t;
  1617. }
  1618. static statement *parse_statement(state_t *state, block *parent_block) {
  1619. switch (current(state).kind) {
  1620. case TOKEN_IF: {
  1621. advance_state(state);
  1622. match_token(state, TOKEN_LEFT_PAREN, "Expected an opening bracket");
  1623. advance_state(state);
  1624. expression *test = parse_expression(state);
  1625. match_token(state, TOKEN_RIGHT_PAREN, "Expected a closing bracket");
  1626. advance_state(state);
  1627. statement *if_block = parse_statement(state, parent_block);
  1628. statement *s = statement_allocate();
  1629. s->kind = STATEMENT_IF;
  1630. s->iffy.test = test;
  1631. s->iffy.if_block = if_block;
  1632. s->iffy.else_size = 0;
  1633. while (current(state).kind == TOKEN_ELSE) {
  1634. advance_state(state);
  1635. if (current(state).kind == TOKEN_IF) {
  1636. advance_state(state);
  1637. match_token(state, TOKEN_LEFT_PAREN, "Expected an opening bracket");
  1638. advance_state(state);
  1639. expression *test = parse_expression(state);
  1640. match_token(state, TOKEN_RIGHT_PAREN, "Expected a closing bracket");
  1641. advance_state(state);
  1642. statement *if_block = parse_statement(state, parent_block);
  1643. s->iffy.else_tests[s->iffy.else_size] = test;
  1644. s->iffy.else_blocks[s->iffy.else_size] = if_block;
  1645. }
  1646. else {
  1647. statement *else_block = parse_statement(state, parent_block);
  1648. s->iffy.else_tests[s->iffy.else_size] = NULL;
  1649. s->iffy.else_blocks[s->iffy.else_size] = else_block;
  1650. }
  1651. s->iffy.else_size += 1;
  1652. assert(s->iffy.else_size < 64);
  1653. }
  1654. return s;
  1655. }
  1656. case TOKEN_WHILE: {
  1657. advance_state(state);
  1658. match_token(state, TOKEN_LEFT_PAREN, "Expected an opening bracket");
  1659. advance_state(state);
  1660. expression *test = parse_expression(state);
  1661. match_token(state, TOKEN_RIGHT_PAREN, "Expected a closing bracket");
  1662. advance_state(state);
  1663. statement *while_block = parse_statement(state, parent_block);
  1664. statement *s = statement_allocate();
  1665. s->kind = STATEMENT_WHILE;
  1666. s->whiley.test = test;
  1667. s->whiley.while_block = while_block;
  1668. return s;
  1669. }
  1670. case TOKEN_DO: {
  1671. advance_state(state);
  1672. statement *do_block = parse_statement(state, parent_block);
  1673. statement *s = statement_allocate();
  1674. s->kind = STATEMENT_DO_WHILE;
  1675. s->whiley.while_block = do_block;
  1676. match_token(state, TOKEN_WHILE, "Expected \"while\"");
  1677. advance_state(state);
  1678. match_token(state, TOKEN_LEFT_PAREN, "Expected an opening bracket");
  1679. advance_state(state);
  1680. expression *test = parse_expression(state);
  1681. match_token(state, TOKEN_RIGHT_PAREN, "Expected a closing bracket");
  1682. advance_state(state);
  1683. s->whiley.test = test;
  1684. match_token(state, TOKEN_SEMICOLON, "Expected a semicolon");
  1685. advance_state(state);
  1686. return s;
  1687. }
  1688. case TOKEN_FOR: {
  1689. statements outer_block_statements;
  1690. statements_init(&outer_block_statements);
  1691. statement *outer_block = statement_allocate();
  1692. outer_block->kind = STATEMENT_BLOCK;
  1693. outer_block->block.parent = parent_block;
  1694. outer_block->block.vars.size = 0;
  1695. outer_block->block.statements = outer_block_statements;
  1696. advance_state(state);
  1697. match_token(state, TOKEN_LEFT_PAREN, "Expected an opening bracket");
  1698. advance_state(state);
  1699. statement *pre = parse_statement(state, &outer_block->block);
  1700. statements_add(&outer_block->block.statements, pre);
  1701. expression *test = parse_expression(state);
  1702. match_token(state, TOKEN_SEMICOLON, "Expected a semicolon");
  1703. advance_state(state);
  1704. expression *post_expression = parse_expression(state);
  1705. match_token(state, TOKEN_RIGHT_PAREN, "Expected a closing bracket");
  1706. advance_state(state);
  1707. statement *inner_block = parse_statement(state, &outer_block->block);
  1708. statement *post_statement = statement_allocate();
  1709. post_statement->kind = STATEMENT_EXPRESSION;
  1710. post_statement->expression = post_expression;
  1711. statements_add(&inner_block->block.statements, post_statement);
  1712. statement *s = statement_allocate();
  1713. s->kind = STATEMENT_WHILE;
  1714. s->whiley.test = test;
  1715. s->whiley.while_block = inner_block;
  1716. statements_add(&outer_block->block.statements, s);
  1717. return outer_block;
  1718. }
  1719. case TOKEN_LEFT_CURLY: {
  1720. return parse_block(state, parent_block);
  1721. }
  1722. case TOKEN_VAR: {
  1723. advance_state(state);
  1724. match_token_identifier(state);
  1725. token name = current(state);
  1726. advance_state(state);
  1727. match_token(state, TOKEN_COLON, "Expected a colon");
  1728. advance_state(state);
  1729. type_ref type = parse_type_ref(state);
  1730. expression *init = NULL;
  1731. if (current(state).kind == TOKEN_OPERATOR) {
  1732. check(current(state).op == OPERATOR_ASSIGN, state->context, "Expected an assign");
  1733. advance_state(state);
  1734. init = parse_expression(state);
  1735. }
  1736. match_token(state, TOKEN_SEMICOLON, "Expected a semicolon");
  1737. advance_state(state);
  1738. statement *statement = statement_allocate();
  1739. statement->kind = STATEMENT_LOCAL_VARIABLE;
  1740. statement->local_variable.var.name = name.identifier;
  1741. statement->local_variable.var.type = type;
  1742. statement->local_variable.var.variable_id = 0;
  1743. statement->local_variable.init = init;
  1744. return statement;
  1745. }
  1746. case TOKEN_RETURN: {
  1747. advance_state(state);
  1748. expression *expr = parse_expression(state);
  1749. match_token(state, TOKEN_SEMICOLON, "Expected a semicolon");
  1750. advance_state(state);
  1751. statement *statement = statement_allocate();
  1752. statement->kind = STATEMENT_RETURN_EXPRESSION;
  1753. statement->expression = expr;
  1754. return statement;
  1755. }
  1756. default: {
  1757. expression *expr = parse_expression(state);
  1758. match_token(state, TOKEN_SEMICOLON, "Expected a semicolon");
  1759. advance_state(state);
  1760. statement *statement = statement_allocate();
  1761. statement->kind = STATEMENT_EXPRESSION;
  1762. statement->expression = expr;
  1763. return statement;
  1764. }
  1765. }
  1766. }
  1767. static expression *parse_assign(state_t *state);
  1768. static expression *parse_expression(state_t *state) {
  1769. return parse_assign(state);
  1770. }
  1771. static expression *parse_logical(state_t *state);
  1772. static expression *parse_assign(state_t *state) {
  1773. expression *expr = parse_logical(state);
  1774. bool done = false;
  1775. while (!done) {
  1776. if (current(state).kind == TOKEN_OPERATOR) {
  1777. operatorr op = current(state).op;
  1778. if (op == OPERATOR_ASSIGN || op == OPERATOR_MINUS_ASSIGN || op == OPERATOR_PLUS_ASSIGN || op == OPERATOR_DIVIDE_ASSIGN ||
  1779. op == OPERATOR_MULTIPLY_ASSIGN) {
  1780. advance_state(state);
  1781. expression *right = parse_logical(state);
  1782. expression *expression = expression_allocate();
  1783. expression->kind = EXPRESSION_BINARY;
  1784. expression->binary.left = expr;
  1785. expression->binary.op = op;
  1786. expression->binary.right = right;
  1787. expr = expression;
  1788. }
  1789. else {
  1790. done = true;
  1791. }
  1792. }
  1793. else {
  1794. done = true;
  1795. }
  1796. }
  1797. return expr;
  1798. }
  1799. static expression *parse_bitwise(state_t *state);
  1800. static expression *parse_logical(state_t *state) {
  1801. expression *expr = parse_bitwise(state);
  1802. bool done = false;
  1803. while (!done) {
  1804. if (current(state).kind == TOKEN_OPERATOR) {
  1805. operatorr op = current(state).op;
  1806. if (op == OPERATOR_OR || op == OPERATOR_AND) {
  1807. advance_state(state);
  1808. expression *right = parse_bitwise(state);
  1809. expression *expression = expression_allocate();
  1810. expression->kind = EXPRESSION_BINARY;
  1811. expression->binary.left = expr;
  1812. expression->binary.op = op;
  1813. expression->binary.right = right;
  1814. expr = expression;
  1815. }
  1816. else {
  1817. done = true;
  1818. }
  1819. }
  1820. else {
  1821. done = true;
  1822. }
  1823. }
  1824. return expr;
  1825. }
  1826. static expression *parse_equality(state_t *state);
  1827. static expression *parse_bitwise(state_t *state) {
  1828. expression *expr = parse_equality(state);
  1829. bool done = false;
  1830. while (!done) {
  1831. if (current(state).kind == TOKEN_OPERATOR) {
  1832. operatorr op = current(state).op;
  1833. if (op == OPERATOR_BITWISE_XOR || op == OPERATOR_BITWISE_OR || op == OPERATOR_BITWISE_AND) {
  1834. advance_state(state);
  1835. expression *right = parse_equality(state);
  1836. expression *expression = expression_allocate();
  1837. expression->kind = EXPRESSION_BINARY;
  1838. expression->binary.left = expr;
  1839. expression->binary.op = op;
  1840. expression->binary.right = right;
  1841. expr = expression;
  1842. }
  1843. else {
  1844. done = true;
  1845. }
  1846. }
  1847. else {
  1848. done = true;
  1849. }
  1850. }
  1851. return expr;
  1852. }
  1853. static expression *parse_comparison(state_t *state);
  1854. static expression *parse_equality(state_t *state) {
  1855. expression *expr = parse_comparison(state);
  1856. bool done = false;
  1857. while (!done) {
  1858. if (current(state).kind == TOKEN_OPERATOR) {
  1859. operatorr op = current(state).op;
  1860. if (op == OPERATOR_EQUALS || op == OPERATOR_NOT_EQUALS) {
  1861. advance_state(state);
  1862. expression *right = parse_comparison(state);
  1863. expression *expression = expression_allocate();
  1864. expression->kind = EXPRESSION_BINARY;
  1865. expression->binary.left = expr;
  1866. expression->binary.op = op;
  1867. expression->binary.right = right;
  1868. expr = expression;
  1869. }
  1870. else {
  1871. done = true;
  1872. }
  1873. }
  1874. else {
  1875. done = true;
  1876. }
  1877. }
  1878. return expr;
  1879. }
  1880. static expression *parse_shift(state_t *state);
  1881. static expression *parse_comparison(state_t *state) {
  1882. expression *expr = parse_shift(state);
  1883. bool done = false;
  1884. while (!done) {
  1885. if (current(state).kind == TOKEN_OPERATOR) {
  1886. operatorr op = current(state).op;
  1887. if (op == OPERATOR_GREATER || op == OPERATOR_GREATER_EQUAL || op == OPERATOR_LESS || op == OPERATOR_LESS_EQUAL) {
  1888. advance_state(state);
  1889. expression *right = parse_shift(state);
  1890. expression *expression = expression_allocate();
  1891. expression->kind = EXPRESSION_BINARY;
  1892. expression->binary.left = expr;
  1893. expression->binary.op = op;
  1894. expression->binary.right = right;
  1895. expr = expression;
  1896. }
  1897. else {
  1898. done = true;
  1899. }
  1900. }
  1901. else {
  1902. done = true;
  1903. }
  1904. }
  1905. return expr;
  1906. }
  1907. static expression *parse_addition(state_t *state);
  1908. static expression *parse_shift(state_t *state) {
  1909. expression *expr = parse_addition(state);
  1910. bool done = false;
  1911. while (!done) {
  1912. if (current(state).kind == TOKEN_OPERATOR) {
  1913. operatorr op = current(state).op;
  1914. if (op == OPERATOR_LEFT_SHIFT || op == OPERATOR_RIGHT_SHIFT) {
  1915. advance_state(state);
  1916. expression *right = parse_addition(state);
  1917. expression *expression = expression_allocate();
  1918. expression->kind = EXPRESSION_BINARY;
  1919. expression->binary.left = expr;
  1920. expression->binary.op = op;
  1921. expression->binary.right = right;
  1922. expr = expression;
  1923. }
  1924. else {
  1925. done = true;
  1926. }
  1927. }
  1928. else {
  1929. done = true;
  1930. }
  1931. }
  1932. return expr;
  1933. }
  1934. static expression *parse_multiplication(state_t *state);
  1935. static expression *parse_addition(state_t *state) {
  1936. expression *expr = parse_multiplication(state);
  1937. bool done = false;
  1938. while (!done) {
  1939. if (current(state).kind == TOKEN_OPERATOR) {
  1940. operatorr op = current(state).op;
  1941. if (op == OPERATOR_MINUS || op == OPERATOR_PLUS) {
  1942. advance_state(state);
  1943. expression *right = parse_multiplication(state);
  1944. expression *expression = expression_allocate();
  1945. expression->kind = EXPRESSION_BINARY;
  1946. expression->binary.left = expr;
  1947. expression->binary.op = op;
  1948. expression->binary.right = right;
  1949. expr = expression;
  1950. }
  1951. else {
  1952. done = true;
  1953. }
  1954. }
  1955. else {
  1956. done = true;
  1957. }
  1958. }
  1959. return expr;
  1960. }
  1961. static expression *parse_unary(state_t *state);
  1962. static expression *parse_multiplication(state_t *state) {
  1963. expression *expr = parse_unary(state);
  1964. bool done = false;
  1965. while (!done) {
  1966. if (current(state).kind == TOKEN_OPERATOR) {
  1967. operatorr op = current(state).op;
  1968. if (op == OPERATOR_DIVIDE || op == OPERATOR_MULTIPLY || op == OPERATOR_MOD) {
  1969. advance_state(state);
  1970. expression *right = parse_unary(state);
  1971. expression *expression = expression_allocate();
  1972. expression->kind = EXPRESSION_BINARY;
  1973. expression->binary.left = expr;
  1974. expression->binary.op = op;
  1975. expression->binary.right = right;
  1976. expr = expression;
  1977. }
  1978. else {
  1979. done = true;
  1980. }
  1981. }
  1982. else {
  1983. done = true;
  1984. }
  1985. }
  1986. return expr;
  1987. }
  1988. static expression *parse_primary(state_t *state);
  1989. static expression *parse_unary(state_t *state) {
  1990. bool done = false;
  1991. while (!done) {
  1992. if (current(state).kind == TOKEN_OPERATOR) {
  1993. operatorr op = current(state).op;
  1994. if (op == OPERATOR_NOT || op == OPERATOR_MINUS) {
  1995. advance_state(state);
  1996. expression *right = parse_unary(state);
  1997. expression *expression = expression_allocate();
  1998. expression->kind = EXPRESSION_UNARY;
  1999. expression->unary.op = op;
  2000. expression->unary.right = right;
  2001. return expression;
  2002. }
  2003. else {
  2004. done = true;
  2005. }
  2006. }
  2007. else {
  2008. done = true;
  2009. }
  2010. }
  2011. return parse_primary(state);
  2012. }
  2013. static expression *parse_member_or_element_access(state_t *state, expression *of) {
  2014. if (current(state).kind == TOKEN_DOT) {
  2015. advance_state(state);
  2016. match_token(state, TOKEN_IDENTIFIER, "Expected an identifier");
  2017. token token = current(state);
  2018. advance_state(state);
  2019. expression *member = expression_allocate();
  2020. member->kind = EXPRESSION_MEMBER;
  2021. member->member.of = of;
  2022. member->member.member_name = token.identifier;
  2023. expression *sub = parse_member_or_element_access(state, member);
  2024. return sub;
  2025. }
  2026. else if (current(state).kind == TOKEN_LEFT_SQUARE) {
  2027. advance_state(state);
  2028. expression *index = parse_expression(state);
  2029. match_token(state, TOKEN_RIGHT_SQUARE, "Expected a closing square bracket");
  2030. advance_state(state);
  2031. expression *element = expression_allocate();
  2032. element->kind = EXPRESSION_ELEMENT;
  2033. element->element.of = of;
  2034. element->element.element_index = index;
  2035. expression *sub = parse_member_or_element_access(state, element);
  2036. return sub;
  2037. }
  2038. if (current(state).kind == TOKEN_LEFT_PAREN) {
  2039. error(state->context, "Function members not supported.");
  2040. }
  2041. return of;
  2042. }
  2043. static expression *parse_call(state_t *state, name_id func_name);
  2044. static expression *parse_primary(state_t *state) {
  2045. expression *left = NULL;
  2046. switch (current(state).kind) {
  2047. case TOKEN_BOOLEAN: {
  2048. bool value = current(state).boolean;
  2049. advance_state(state);
  2050. left = expression_allocate();
  2051. left->kind = EXPRESSION_BOOLEAN;
  2052. left->boolean = value;
  2053. break;
  2054. }
  2055. case TOKEN_FLOAT: {
  2056. double value = current(state).number;
  2057. advance_state(state);
  2058. left = expression_allocate();
  2059. left->kind = EXPRESSION_FLOAT;
  2060. left->number = value;
  2061. break;
  2062. }
  2063. case TOKEN_INT: {
  2064. double value = current(state).number;
  2065. advance_state(state);
  2066. left = expression_allocate();
  2067. left->kind = EXPRESSION_INT;
  2068. left->number = value;
  2069. break;
  2070. }
  2071. /*case TOKEN_STRING: {
  2072. token token = current(state);
  2073. advance_state(state);
  2074. left = expression_allocate();
  2075. left->kind = EXPRESSION_STRING;
  2076. left->string = add_name(token.string);
  2077. break;
  2078. }*/
  2079. case TOKEN_IDENTIFIER: {
  2080. token token = current(state);
  2081. advance_state(state);
  2082. if (current(state).kind == TOKEN_LEFT_PAREN) {
  2083. left = parse_call(state, token.identifier);
  2084. }
  2085. else {
  2086. expression *var = expression_allocate();
  2087. var->kind = EXPRESSION_VARIABLE;
  2088. var->variable = token.identifier;
  2089. left = var;
  2090. }
  2091. break;
  2092. }
  2093. case TOKEN_LEFT_PAREN: {
  2094. advance_state(state);
  2095. expression *expr = parse_expression(state);
  2096. match_token(state, TOKEN_RIGHT_PAREN, "Expected a closing bracket");
  2097. advance_state(state);
  2098. left = expression_allocate();
  2099. left->kind = EXPRESSION_GROUPING;
  2100. left->grouping = expr;
  2101. break;
  2102. }
  2103. default:
  2104. error(state->context, "Unexpected token");
  2105. return NULL;
  2106. }
  2107. return parse_member_or_element_access(state, left);
  2108. }
  2109. static expressions parse_parameters(state_t *state) {
  2110. expressions e;
  2111. e.size = 0;
  2112. if (current(state).kind == TOKEN_RIGHT_PAREN) {
  2113. advance_state(state);
  2114. return e;
  2115. }
  2116. for (;;) {
  2117. e.e[e.size] = parse_expression(state);
  2118. e.size += 1;
  2119. if (current(state).kind == TOKEN_COMMA) {
  2120. advance_state(state);
  2121. }
  2122. else {
  2123. match_token(state, TOKEN_RIGHT_PAREN, "Expected a closing bracket");
  2124. advance_state(state);
  2125. return e;
  2126. }
  2127. }
  2128. }
  2129. static expression *parse_call(state_t *state, name_id func_name) {
  2130. match_token(state, TOKEN_LEFT_PAREN, "Expected an opening bracket");
  2131. advance_state(state);
  2132. expression *call = NULL;
  2133. call = expression_allocate();
  2134. call->kind = EXPRESSION_CALL;
  2135. call->call.func_name = func_name;
  2136. call->call.parameters = parse_parameters(state);
  2137. return parse_member_or_element_access(state, call);
  2138. }
  2139. static definition parse_struct_inner(state_t *state, name_id name) {
  2140. match_token(state, TOKEN_LEFT_CURLY, "Expected an opening curly bracket");
  2141. advance_state(state);
  2142. token member_names[MAX_MEMBERS];
  2143. type_ref type_refs[MAX_MEMBERS];
  2144. token member_values[MAX_MEMBERS];
  2145. size_t count = 0;
  2146. while (current(state).kind != TOKEN_RIGHT_CURLY) {
  2147. debug_context context = {0};
  2148. check(count < MAX_MEMBERS, context, "Out of members");
  2149. match_token(state, TOKEN_IDENTIFIER, "Expected an identifier");
  2150. member_names[count] = current(state);
  2151. advance_state(state);
  2152. if (current(state).kind == TOKEN_COLON) {
  2153. advance_state(state);
  2154. type_refs[count] = parse_type_ref(state);
  2155. }
  2156. else {
  2157. type_ref t;
  2158. t.type = NO_TYPE;
  2159. t.unresolved.name = NO_NAME;
  2160. t.unresolved.array_size = 0;
  2161. type_refs[count] = t;
  2162. }
  2163. if (current(state).kind == TOKEN_OPERATOR && current(state).op == OPERATOR_ASSIGN) {
  2164. advance_state(state);
  2165. if (current(state).kind == TOKEN_BOOLEAN || current(state).kind == TOKEN_FLOAT || current(state).kind == TOKEN_INT ||
  2166. current(state).kind == TOKEN_IDENTIFIER) {
  2167. member_values[count] = current(state);
  2168. advance_state(state);
  2169. if (current(state).kind == TOKEN_LEFT_PAREN) {
  2170. advance_state(state);
  2171. match_token(state, TOKEN_RIGHT_PAREN, "Expected a right paren");
  2172. advance_state(state);
  2173. }
  2174. }
  2175. else {
  2176. debug_context context = {0};
  2177. error(context, "Unsupported assign in struct");
  2178. }
  2179. }
  2180. else {
  2181. member_values[count].kind = TOKEN_NONE;
  2182. member_values[count].identifier = NO_NAME;
  2183. }
  2184. match_token(state, TOKEN_SEMICOLON, "Expected a semicolon");
  2185. advance_state(state);
  2186. ++count;
  2187. }
  2188. advance_state(state);
  2189. definition definition;
  2190. definition.kind = DEFINITION_STRUCT;
  2191. definition.type = add_type(name);
  2192. type *s = get_type(definition.type);
  2193. for (size_t i = 0; i < count; ++i) {
  2194. member member;
  2195. member.name = member_names[i].identifier;
  2196. member.value = member_values[i];
  2197. if (member.value.kind != TOKEN_NONE) {
  2198. if (member.value.kind == TOKEN_BOOLEAN) {
  2199. init_type_ref(&member.type, add_name("bool"));
  2200. }
  2201. else if (member.value.kind == TOKEN_FLOAT) {
  2202. init_type_ref(&member.type, add_name("float"));
  2203. }
  2204. else if (member.value.kind == TOKEN_INT) {
  2205. init_type_ref(&member.type, add_name("int"));
  2206. }
  2207. else if (member.value.kind == TOKEN_IDENTIFIER) {
  2208. global *g = find_global(member.value.identifier);
  2209. if (g != NULL && g->name != NO_NAME) {
  2210. init_type_ref(&member.type, get_type(g->type)->name);
  2211. }
  2212. else {
  2213. init_type_ref(&member.type, add_name("fun"));
  2214. }
  2215. }
  2216. else {
  2217. debug_context context = {0};
  2218. error(context, "Unsupported value in struct");
  2219. }
  2220. }
  2221. else {
  2222. member.type = type_refs[i];
  2223. }
  2224. s->members.m[i] = member;
  2225. }
  2226. s->members.size = count;
  2227. return definition;
  2228. }
  2229. static definition parse_struct(state_t *state) {
  2230. advance_state(state);
  2231. match_token(state, TOKEN_IDENTIFIER, "Expected an identifier");
  2232. token name = current(state);
  2233. advance_state(state);
  2234. return parse_struct_inner(state, name.identifier);
  2235. }
  2236. static definition parse_function(state_t *state) {
  2237. advance_state(state);
  2238. match_token(state, TOKEN_IDENTIFIER, "Expected an identifier");
  2239. token name = current(state);
  2240. advance_state(state);
  2241. match_token(state, TOKEN_LEFT_PAREN, "Expected an opening bracket");
  2242. advance_state(state);
  2243. uint8_t parameters_size = 0;
  2244. name_id param_names[256] = {0};
  2245. type_ref param_types[256] = {0};
  2246. name_id param_attributes[256] = {0};
  2247. while (current(state).kind != TOKEN_RIGHT_PAREN) {
  2248. if (current(state).kind == TOKEN_HASH) {
  2249. advance_state(state);
  2250. match_token(state, TOKEN_LEFT_SQUARE, "Expected an opening square bracket");
  2251. advance_state(state);
  2252. match_token(state, TOKEN_IDENTIFIER, "Expected an identifier");
  2253. token attribute_name = current(state);
  2254. param_attributes[parameters_size] = attribute_name.identifier;
  2255. advance_state(state);
  2256. match_token(state, TOKEN_RIGHT_SQUARE, "Expected a closing square bracket");
  2257. advance_state(state);
  2258. }
  2259. match_token(state, TOKEN_IDENTIFIER, "Expected an identifier");
  2260. param_names[parameters_size] = current(state).identifier;
  2261. advance_state(state);
  2262. match_token(state, TOKEN_COLON, "Expected a colon");
  2263. advance_state(state);
  2264. param_types[parameters_size] = parse_type_ref(state);
  2265. if (current(state).kind == TOKEN_COMMA) {
  2266. advance_state(state);
  2267. }
  2268. parameters_size += 1;
  2269. }
  2270. match_token(state, TOKEN_RIGHT_PAREN, "Expected a closing bracket");
  2271. advance_state(state);
  2272. match_token(state, TOKEN_COLON, "Expected a colon");
  2273. advance_state(state);
  2274. type_ref return_type = parse_type_ref(state);
  2275. statement *block = parse_block(state, NULL);
  2276. definition d;
  2277. d.kind = DEFINITION_FUNCTION;
  2278. d.function = add_function(name.identifier);
  2279. function *f = _get_function(d.function);
  2280. f->return_type = return_type;
  2281. f->parameters_size = parameters_size;
  2282. for (uint8_t parameter_index = 0; parameter_index < parameters_size; ++parameter_index) {
  2283. f->parameter_names[parameter_index] = param_names[parameter_index];
  2284. f->parameter_types[parameter_index] = param_types[parameter_index];
  2285. f->parameter_attributes[parameter_index] = param_attributes[parameter_index];
  2286. }
  2287. f->block = block;
  2288. return d;
  2289. }
  2290. static definition parse_const(state_t *state, attribute_list attributes) {
  2291. advance_state(state);
  2292. match_token(state, TOKEN_IDENTIFIER, "Expected an identifier");
  2293. token name = current(state);
  2294. advance_state(state);
  2295. match_token(state, TOKEN_COLON, "Expected a colon");
  2296. advance_state(state);
  2297. name_id type_name = NO_NAME;
  2298. type_id type = NO_TYPE;
  2299. name_id format_name = NO_NAME;
  2300. if (current(state).kind == TOKEN_LEFT_CURLY) {
  2301. type = parse_struct_inner(state, NO_NAME).type;
  2302. }
  2303. else {
  2304. match_token(state, TOKEN_IDENTIFIER, "Expected an identifier");
  2305. type_name = current(state).identifier;
  2306. advance_state(state);
  2307. }
  2308. bool array = false;
  2309. uint32_t array_size = UINT32_MAX;
  2310. if (current(state).kind == TOKEN_LEFT_SQUARE) {
  2311. array = true;
  2312. advance_state(state);
  2313. if (current(state).kind == TOKEN_INT) {
  2314. array_size = (uint32_t)current(state).number;
  2315. advance_state(state);
  2316. }
  2317. match_token(state, TOKEN_RIGHT_SQUARE, "Expected a right square bracket");
  2318. advance_state(state);
  2319. }
  2320. expression *value = NULL;
  2321. if (current(state).kind == TOKEN_OPERATOR && current(state).op == OPERATOR_ASSIGN) {
  2322. advance_state(state);
  2323. value = parse_expression(state);
  2324. }
  2325. if (current(state).kind == TOKEN_OPERATOR && current(state).op == OPERATOR_LESS) {
  2326. advance_state(state);
  2327. match_token(state, TOKEN_IDENTIFIER, "Expected an identifier");
  2328. format_name = current(state).identifier;
  2329. advance_state(state);
  2330. if (current(state).kind == TOKEN_LEFT_PAREN) {
  2331. advance_state(state);
  2332. match_token(state, TOKEN_RIGHT_PAREN, "Expected a right paren");
  2333. advance_state(state);
  2334. }
  2335. if (current(state).kind != TOKEN_OPERATOR || current(state).op != OPERATOR_GREATER) {
  2336. error(state->context, "Expected a greater than");
  2337. }
  2338. advance_state(state);
  2339. }
  2340. match_token(state, TOKEN_SEMICOLON, "Expected a semicolon");
  2341. advance_state(state);
  2342. definition d = {0};
  2343. if (type_name == NO_NAME) {
  2344. debug_context context = {0};
  2345. check(type != NO_TYPE, context, "Const has no type");
  2346. d.kind = DEFINITION_CONST_CUSTOM;
  2347. d.global = add_global(type, attributes, name.identifier);
  2348. }
  2349. else if (type_name == add_name("float")) {
  2350. debug_context context = {0};
  2351. check(value != NULL, context, "const float requires an initialization value");
  2352. check(value->kind == EXPRESSION_FLOAT || value->kind == EXPRESSION_INT, context, "const float requires a number");
  2353. global_value float_value;
  2354. float_value.kind = GLOBAL_VALUE_FLOAT;
  2355. float_value.value.floats[0] = (float)value->number;
  2356. d.kind = DEFINITION_CONST_BASIC;
  2357. d.global = add_global_with_value(_float_id, attributes, name.identifier, float_value);
  2358. }
  2359. else {
  2360. debug_context context = {0};
  2361. error(context, "Unsupported global");
  2362. }
  2363. return d;
  2364. }
  2365. static token tokens_get(tokens *tokens, size_t index) {
  2366. debug_context context = {0};
  2367. check(tokens->current_size > index, context, "Token index out of bounds");
  2368. return tokens->t[index];
  2369. }
  2370. static bool is_num(char ch, char chch) {
  2371. return (ch >= '0' && ch <= '9') || (ch == '-' && chch >= '0' && chch <= '9');
  2372. }
  2373. static bool is_op(char ch) {
  2374. return ch == '&' || ch == '|' || ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '=' || ch == '!' || ch == '<' || ch == '>' || ch == '%' ||
  2375. ch == '^';
  2376. }
  2377. static bool is_whitespace(char ch) {
  2378. return ch == ' ' || (ch >= 9 && ch <= 13);
  2379. }
  2380. static void tokenizer_state_init(debug_context *context, tokenizer_state *state, const char *source) {
  2381. state->line = state->column = 0;
  2382. state->iterator = source;
  2383. state->next = *state->iterator;
  2384. if (*state->iterator != 0) {
  2385. state->iterator += 1;
  2386. }
  2387. state->next_next = *state->iterator;
  2388. state->line_end = false;
  2389. context->column = 0;
  2390. context->line = 0;
  2391. }
  2392. static void tokenizer_state_advance(debug_context *context, tokenizer_state *state) {
  2393. state->next = state->next_next;
  2394. if (*state->iterator != 0) {
  2395. state->iterator += 1;
  2396. }
  2397. state->next_next = *state->iterator;
  2398. if (state->line_end) {
  2399. state->line_end = false;
  2400. state->line += 1;
  2401. state->column = 0;
  2402. }
  2403. else {
  2404. state->column += 1;
  2405. }
  2406. if (state->next == '\n') {
  2407. state->line_end = true;
  2408. }
  2409. context->column = state->column;
  2410. context->line = state->line;
  2411. }
  2412. static void tokenizer_buffer_init(tokenizer_buffer *buffer) {
  2413. buffer->max_size = 1024 * 1024;
  2414. buffer->buf = (char *)malloc(buffer->max_size);
  2415. buffer->current_size = 0;
  2416. buffer->column = buffer->line = 0;
  2417. }
  2418. static void tokenizer_buffer_reset(tokenizer_buffer *buffer, tokenizer_state *state) {
  2419. buffer->current_size = 0;
  2420. buffer->column = state->column;
  2421. buffer->line = state->line;
  2422. }
  2423. static void tokenizer_buffer_add(tokenizer_buffer *buffer, char ch) {
  2424. debug_context context = {0};
  2425. check(buffer->current_size < buffer->max_size, context, "Token buffer is too small");
  2426. buffer->buf[buffer->current_size] = ch;
  2427. buffer->current_size += 1;
  2428. }
  2429. static bool tokenizer_buffer_equals(tokenizer_buffer *buffer, const char *str) {
  2430. buffer->buf[buffer->current_size] = 0;
  2431. return strcmp(buffer->buf, str) == 0;
  2432. }
  2433. static name_id tokenizer_buffer_to_name(tokenizer_buffer *buffer) {
  2434. debug_context context = {0};
  2435. check(buffer->current_size < buffer->max_size, context, "Token buffer is too small");
  2436. buffer->buf[buffer->current_size] = 0;
  2437. buffer->current_size += 1;
  2438. return add_name(buffer->buf);
  2439. }
  2440. static double tokenizer_buffer_parse_number(tokenizer_buffer *buffer) {
  2441. buffer->buf[buffer->current_size] = 0;
  2442. return strtod(buffer->buf, NULL);
  2443. }
  2444. static token token_create(int kind, tokenizer_state *state) {
  2445. token token;
  2446. token.kind = kind;
  2447. token.column = state->column;
  2448. token.line = state->line;
  2449. return token;
  2450. }
  2451. static void tokens_init(tokens *tokens) {
  2452. tokens->max_size = 1024 * 1024;
  2453. tokens->t = malloc(tokens->max_size * sizeof(token));
  2454. tokens->current_size = 0;
  2455. }
  2456. static void tokens_add(tokens *tokens, token token) {
  2457. tokens->t[tokens->current_size] = token;
  2458. tokens->current_size += 1;
  2459. debug_context context = {0};
  2460. check(tokens->current_size <= tokens->max_size, context, "Out of tokens");
  2461. }
  2462. static void tokens_add_identifier(tokenizer_state *state, tokens *tokens, tokenizer_buffer *buffer) {
  2463. token token;
  2464. if (tokenizer_buffer_equals(buffer, "true")) {
  2465. token = token_create(TOKEN_BOOLEAN, state);
  2466. token.boolean = true;
  2467. }
  2468. else if (tokenizer_buffer_equals(buffer, "false")) {
  2469. token = token_create(TOKEN_BOOLEAN, state);
  2470. token.boolean = false;
  2471. }
  2472. else if (tokenizer_buffer_equals(buffer, "if")) {
  2473. token = token_create(TOKEN_IF, state);
  2474. }
  2475. else if (tokenizer_buffer_equals(buffer, "else")) {
  2476. token = token_create(TOKEN_ELSE, state);
  2477. }
  2478. else if (tokenizer_buffer_equals(buffer, "while")) {
  2479. token = token_create(TOKEN_WHILE, state);
  2480. }
  2481. else if (tokenizer_buffer_equals(buffer, "do")) {
  2482. token = token_create(TOKEN_DO, state);
  2483. }
  2484. else if (tokenizer_buffer_equals(buffer, "for")) {
  2485. token = token_create(TOKEN_FOR, state);
  2486. }
  2487. else if (tokenizer_buffer_equals(buffer, "in")) {
  2488. token = token_create(TOKEN_IN, state);
  2489. }
  2490. else if (tokenizer_buffer_equals(buffer, "struct")) {
  2491. token = token_create(TOKEN_STRUCT, state);
  2492. }
  2493. else if (tokenizer_buffer_equals(buffer, "fun")) {
  2494. token = token_create(TOKEN_FUNCTION, state);
  2495. }
  2496. else if (tokenizer_buffer_equals(buffer, "var")) {
  2497. token = token_create(TOKEN_VAR, state);
  2498. }
  2499. else if (tokenizer_buffer_equals(buffer, "const")) {
  2500. token = token_create(TOKEN_CONST, state);
  2501. }
  2502. else if (tokenizer_buffer_equals(buffer, "return")) {
  2503. token = token_create(TOKEN_RETURN, state);
  2504. }
  2505. else {
  2506. token = token_create(TOKEN_IDENTIFIER, state);
  2507. token.identifier = tokenizer_buffer_to_name(buffer);
  2508. }
  2509. token.column = buffer->column;
  2510. token.line = buffer->line;
  2511. tokens_add(tokens, token);
  2512. }
  2513. tokens _tokenize(const char *filename, const char *source) {
  2514. mode mode = MODE_SELECT;
  2515. bool number_has_dot = false;
  2516. tokens tokens;
  2517. tokens_init(&tokens);
  2518. debug_context context = {0};
  2519. context.filename = filename;
  2520. tokenizer_state state;
  2521. tokenizer_state_init(&context, &state, source);
  2522. tokenizer_buffer buffer;
  2523. tokenizer_buffer_init(&buffer);
  2524. for (;;) {
  2525. if (state.next == 0) {
  2526. switch (mode) {
  2527. case MODE_IDENTIFIER:
  2528. tokens_add_identifier(&state, &tokens, &buffer);
  2529. break;
  2530. case MODE_NUMBER: {
  2531. token token = token_create(number_has_dot ? TOKEN_FLOAT : TOKEN_INT, &state);
  2532. token.number = tokenizer_buffer_parse_number(&buffer);
  2533. tokens_add(&tokens, token);
  2534. break;
  2535. }
  2536. case MODE_SELECT:
  2537. case MODE_LINE_COMMENT:
  2538. break;
  2539. // case MODE_STRING:
  2540. // error("Unclosed string", state.column, state.line);
  2541. case MODE_OPERATOR:
  2542. error(context, "File ends with an operator");
  2543. case MODE_COMMENT:
  2544. error(context, "Unclosed comment");
  2545. }
  2546. tokens_add(&tokens, token_create(TOKEN_NONE, &state));
  2547. ////
  2548. free(buffer.buf);
  2549. ////
  2550. return tokens;
  2551. }
  2552. else {
  2553. char ch = (char)state.next;
  2554. switch (mode) {
  2555. case MODE_SELECT: {
  2556. if (ch == '/') {
  2557. if (state.next_next >= 0) {
  2558. char chch = state.next_next;
  2559. switch (chch) {
  2560. case '/':
  2561. mode = MODE_LINE_COMMENT;
  2562. break;
  2563. case '*':
  2564. mode = MODE_COMMENT;
  2565. break;
  2566. default:
  2567. tokenizer_buffer_reset(&buffer, &state);
  2568. tokenizer_buffer_add(&buffer, ch);
  2569. mode = MODE_OPERATOR;
  2570. }
  2571. }
  2572. }
  2573. else if (is_num(ch, state.next_next)) {
  2574. mode = MODE_NUMBER;
  2575. number_has_dot = false;
  2576. tokenizer_buffer_reset(&buffer, &state);
  2577. tokenizer_buffer_add(&buffer, ch);
  2578. }
  2579. else if (is_op(ch)) {
  2580. mode = MODE_OPERATOR;
  2581. tokenizer_buffer_reset(&buffer, &state);
  2582. tokenizer_buffer_add(&buffer, ch);
  2583. }
  2584. else if (is_whitespace(ch)) {
  2585. }
  2586. else if (ch == '(') {
  2587. tokens_add(&tokens, token_create(TOKEN_LEFT_PAREN, &state));
  2588. }
  2589. else if (ch == ')') {
  2590. tokens_add(&tokens, token_create(TOKEN_RIGHT_PAREN, &state));
  2591. }
  2592. else if (ch == '{') {
  2593. tokens_add(&tokens, token_create(TOKEN_LEFT_CURLY, &state));
  2594. }
  2595. else if (ch == '}') {
  2596. tokens_add(&tokens, token_create(TOKEN_RIGHT_CURLY, &state));
  2597. }
  2598. else if (ch == '#') {
  2599. tokens_add(&tokens, token_create(TOKEN_HASH, &state));
  2600. }
  2601. else if (ch == '[') {
  2602. tokens_add(&tokens, token_create(TOKEN_LEFT_SQUARE, &state));
  2603. }
  2604. else if (ch == ']') {
  2605. tokens_add(&tokens, token_create(TOKEN_RIGHT_SQUARE, &state));
  2606. }
  2607. else if (ch == ';') {
  2608. tokens_add(&tokens, token_create(TOKEN_SEMICOLON, &state));
  2609. }
  2610. else if (ch == '.') {
  2611. tokens_add(&tokens, token_create(TOKEN_DOT, &state));
  2612. }
  2613. else if (ch == ':') {
  2614. tokens_add(&tokens, token_create(TOKEN_COLON, &state));
  2615. }
  2616. else if (ch == ',') {
  2617. tokens_add(&tokens, token_create(TOKEN_COMMA, &state));
  2618. }
  2619. else if (ch == '"' || ch == '\'') {
  2620. // mode = MODE_STRING;
  2621. // tokenizer_buffer_reset(&buffer, &state);
  2622. error(context, "Strings are not supported");
  2623. }
  2624. else {
  2625. mode = MODE_IDENTIFIER;
  2626. tokenizer_buffer_reset(&buffer, &state);
  2627. tokenizer_buffer_add(&buffer, ch);
  2628. }
  2629. tokenizer_state_advance(&context, &state);
  2630. break;
  2631. }
  2632. case MODE_LINE_COMMENT: {
  2633. if (ch == '\n') {
  2634. mode = MODE_SELECT;
  2635. }
  2636. tokenizer_state_advance(&context, &state);
  2637. break;
  2638. }
  2639. case MODE_COMMENT: {
  2640. if (ch == '*') {
  2641. if (state.next_next >= 0) {
  2642. char chch = (char)state.next_next;
  2643. if (chch == '/') {
  2644. mode = MODE_SELECT;
  2645. tokenizer_state_advance(&context, &state);
  2646. }
  2647. }
  2648. }
  2649. tokenizer_state_advance(&context, &state);
  2650. break;
  2651. }
  2652. case MODE_NUMBER: {
  2653. if (is_num(ch, 0) || ch == '.') {
  2654. if (ch == '.') {
  2655. number_has_dot = true;
  2656. }
  2657. tokenizer_buffer_add(&buffer, ch);
  2658. tokenizer_state_advance(&context, &state);
  2659. }
  2660. else {
  2661. token token = token_create(number_has_dot ? TOKEN_FLOAT : TOKEN_INT, &state);
  2662. token.number = tokenizer_buffer_parse_number(&buffer);
  2663. tokens_add(&tokens, token);
  2664. mode = MODE_SELECT;
  2665. }
  2666. break;
  2667. }
  2668. case MODE_OPERATOR: {
  2669. char long_op[3];
  2670. long_op[0] = 0;
  2671. if (buffer.current_size == 1) {
  2672. long_op[0] = buffer.buf[0];
  2673. long_op[1] = ch;
  2674. long_op[2] = 0;
  2675. }
  2676. if (strcmp(long_op, "==") == 0 || strcmp(long_op, "!=") == 0 || strcmp(long_op, "<=") == 0 || strcmp(long_op, ">=") == 0 ||
  2677. strcmp(long_op, "||") == 0 || strcmp(long_op, "&&") == 0 || strcmp(long_op, "->") == 0 || strcmp(long_op, "-=") == 0 ||
  2678. strcmp(long_op, "+=") == 0 || strcmp(long_op, "/=") == 0 || strcmp(long_op, "*=") == 0 || strcmp(long_op, "<<") == 0 ||
  2679. strcmp(long_op, ">>") == 0) {
  2680. tokenizer_buffer_add(&buffer, ch);
  2681. tokenizer_state_advance(&context, &state);
  2682. }
  2683. if (tokenizer_buffer_equals(&buffer, "==")) {
  2684. token token = token_create(TOKEN_OPERATOR, &state);
  2685. token.op = OPERATOR_EQUALS;
  2686. tokens_add(&tokens, token);
  2687. }
  2688. else if (tokenizer_buffer_equals(&buffer, "!=")) {
  2689. token token = token_create(TOKEN_OPERATOR, &state);
  2690. token.op = OPERATOR_NOT_EQUALS;
  2691. tokens_add(&tokens, token);
  2692. }
  2693. else if (tokenizer_buffer_equals(&buffer, ">")) {
  2694. token token = token_create(TOKEN_OPERATOR, &state);
  2695. token.op = OPERATOR_GREATER;
  2696. tokens_add(&tokens, token);
  2697. }
  2698. else if (tokenizer_buffer_equals(&buffer, ">=")) {
  2699. token token = token_create(TOKEN_OPERATOR, &state);
  2700. token.op = OPERATOR_GREATER_EQUAL;
  2701. tokens_add(&tokens, token);
  2702. }
  2703. else if (tokenizer_buffer_equals(&buffer, "<")) {
  2704. token token = token_create(TOKEN_OPERATOR, &state);
  2705. token.op = OPERATOR_LESS;
  2706. tokens_add(&tokens, token);
  2707. }
  2708. else if (tokenizer_buffer_equals(&buffer, "<=")) {
  2709. token token = token_create(TOKEN_OPERATOR, &state);
  2710. token.op = OPERATOR_LESS_EQUAL;
  2711. tokens_add(&tokens, token);
  2712. }
  2713. else if (tokenizer_buffer_equals(&buffer, "-=")) {
  2714. token token = token_create(TOKEN_OPERATOR, &state);
  2715. token.op = OPERATOR_MINUS_ASSIGN;
  2716. tokens_add(&tokens, token);
  2717. }
  2718. else if (tokenizer_buffer_equals(&buffer, "+=")) {
  2719. token token = token_create(TOKEN_OPERATOR, &state);
  2720. token.op = OPERATOR_PLUS_ASSIGN;
  2721. tokens_add(&tokens, token);
  2722. }
  2723. else if (tokenizer_buffer_equals(&buffer, "/=")) {
  2724. token token = token_create(TOKEN_OPERATOR, &state);
  2725. token.op = OPERATOR_DIVIDE_ASSIGN;
  2726. tokens_add(&tokens, token);
  2727. }
  2728. else if (tokenizer_buffer_equals(&buffer, "*=")) {
  2729. token token = token_create(TOKEN_OPERATOR, &state);
  2730. token.op = OPERATOR_MULTIPLY_ASSIGN;
  2731. tokens_add(&tokens, token);
  2732. }
  2733. else if (tokenizer_buffer_equals(&buffer, "-")) {
  2734. token token = token_create(TOKEN_OPERATOR, &state);
  2735. token.op = OPERATOR_MINUS;
  2736. tokens_add(&tokens, token);
  2737. }
  2738. else if (tokenizer_buffer_equals(&buffer, "+")) {
  2739. token token = token_create(TOKEN_OPERATOR, &state);
  2740. token.op = OPERATOR_PLUS;
  2741. tokens_add(&tokens, token);
  2742. }
  2743. else if (tokenizer_buffer_equals(&buffer, "/")) {
  2744. token token = token_create(TOKEN_OPERATOR, &state);
  2745. token.op = OPERATOR_DIVIDE;
  2746. tokens_add(&tokens, token);
  2747. }
  2748. else if (tokenizer_buffer_equals(&buffer, "*")) {
  2749. token token = token_create(TOKEN_OPERATOR, &state);
  2750. token.op = OPERATOR_MULTIPLY;
  2751. tokens_add(&tokens, token);
  2752. }
  2753. else if (tokenizer_buffer_equals(&buffer, "!")) {
  2754. token token = token_create(TOKEN_OPERATOR, &state);
  2755. token.op = OPERATOR_NOT;
  2756. tokens_add(&tokens, token);
  2757. }
  2758. else if (tokenizer_buffer_equals(&buffer, "||")) {
  2759. token token = token_create(TOKEN_OPERATOR, &state);
  2760. token.op = OPERATOR_OR;
  2761. tokens_add(&tokens, token);
  2762. }
  2763. else if (tokenizer_buffer_equals(&buffer, "^")) {
  2764. token token = token_create(TOKEN_OPERATOR, &state);
  2765. token.op = OPERATOR_BITWISE_XOR;
  2766. tokens_add(&tokens, token);
  2767. }
  2768. else if (tokenizer_buffer_equals(&buffer, "&")) {
  2769. token token = token_create(TOKEN_OPERATOR, &state);
  2770. token.op = OPERATOR_BITWISE_AND;
  2771. tokens_add(&tokens, token);
  2772. }
  2773. else if (tokenizer_buffer_equals(&buffer, "|")) {
  2774. token token = token_create(TOKEN_OPERATOR, &state);
  2775. token.op = OPERATOR_BITWISE_OR;
  2776. tokens_add(&tokens, token);
  2777. }
  2778. else if (tokenizer_buffer_equals(&buffer, "<<")) {
  2779. token token = token_create(TOKEN_OPERATOR, &state);
  2780. token.op = OPERATOR_LEFT_SHIFT;
  2781. tokens_add(&tokens, token);
  2782. }
  2783. else if (tokenizer_buffer_equals(&buffer, ">>")) {
  2784. token token = token_create(TOKEN_OPERATOR, &state);
  2785. token.op = OPERATOR_RIGHT_SHIFT;
  2786. tokens_add(&tokens, token);
  2787. }
  2788. else if (tokenizer_buffer_equals(&buffer, "&&")) {
  2789. token token = token_create(TOKEN_OPERATOR, &state);
  2790. token.op = OPERATOR_AND;
  2791. tokens_add(&tokens, token);
  2792. }
  2793. else if (tokenizer_buffer_equals(&buffer, "%")) {
  2794. token token = token_create(TOKEN_OPERATOR, &state);
  2795. token.op = OPERATOR_MOD;
  2796. tokens_add(&tokens, token);
  2797. }
  2798. else if (tokenizer_buffer_equals(&buffer, "=")) {
  2799. token token = token_create(TOKEN_OPERATOR, &state);
  2800. token.op = OPERATOR_ASSIGN;
  2801. tokens_add(&tokens, token);
  2802. }
  2803. else {
  2804. error(context, "Weird operator");
  2805. }
  2806. mode = MODE_SELECT;
  2807. break;
  2808. }
  2809. /*case MODE_STRING: {
  2810. if (ch == '"' || ch == '\'') {
  2811. token token = token_create(TOKEN_STRING, &state);
  2812. tokenizer_buffer_copy_to_string(&buffer, token.string);
  2813. token.column = buffer.column;
  2814. token.line = buffer.line;
  2815. tokens_add(&tokens, token);
  2816. tokenizer_state_advance(&state);
  2817. mode = MODE_SELECT;
  2818. }
  2819. else {
  2820. tokenizer_buffer_add(&buffer, ch);
  2821. tokenizer_state_advance(&state);
  2822. }
  2823. break;
  2824. }*/
  2825. case MODE_IDENTIFIER: {
  2826. if (is_whitespace(ch) || is_op(ch) || ch == '(' || ch == ')' || ch == '{' || ch == '}' || ch == '[' || ch == ']' || ch == '"' || ch == '\'' ||
  2827. ch == ';' || ch == '.' || ch == ',' || ch == ':') {
  2828. tokens_add_identifier(&state, &tokens, &buffer);
  2829. mode = MODE_SELECT;
  2830. }
  2831. else {
  2832. tokenizer_buffer_add(&buffer, ch);
  2833. tokenizer_state_advance(&context, &state);
  2834. }
  2835. break;
  2836. }
  2837. }
  2838. }
  2839. }
  2840. }
  2841. static type_ref find_local_var_type(block *b, name_id name) {
  2842. if (b == NULL) {
  2843. type_ref t;
  2844. init_type_ref(&t, NO_NAME);
  2845. return t;
  2846. }
  2847. for (size_t i = 0; i < b->vars.size; ++i) {
  2848. if (b->vars.v[i].name == name) {
  2849. debug_context context = {0};
  2850. check(b->vars.v[i].type.type != NO_TYPE, context, "Local var has no type");
  2851. return b->vars.v[i].type;
  2852. }
  2853. }
  2854. return find_local_var_type(b->parent, name);
  2855. }
  2856. static void resolve_types_in_element(statement *parent_block, expression *element) {
  2857. resolve_types_in_expression(parent_block, element->element.of);
  2858. resolve_types_in_expression(parent_block, element->element.element_index);
  2859. type_id of_type = element->element.of->type.type;
  2860. assert(of_type != NO_TYPE);
  2861. type *of = get_type(of_type);
  2862. if (of->array_size > 0) {
  2863. element->type.type = of->base;
  2864. }
  2865. else {
  2866. debug_context context = {0};
  2867. error(context, "Indexed non-array %s", _get_name(of->name));
  2868. }
  2869. }
  2870. static void resolve_types_in_member(statement *parent_block, expression *member) {
  2871. resolve_types_in_expression(parent_block, member->member.of);
  2872. type_id of_type = member->member.of->type.type;
  2873. name_id member_name = member->member.member_name;
  2874. assert(of_type != NO_TYPE);
  2875. type *of_struct = get_type(of_type);
  2876. for (size_t i = 0; i < of_struct->members.size; ++i) {
  2877. if (of_struct->members.m[i].name == member_name) {
  2878. member->type = of_struct->members.m[i].type;
  2879. return;
  2880. }
  2881. }
  2882. debug_context context = {0};
  2883. error(context, "Member %s not found", _get_name(member_name));
  2884. }
  2885. static bool types_compatible(type_id left, type_id right) {
  2886. if (left == right) {
  2887. return true;
  2888. }
  2889. if ((left == _int_id && right == _float_id) || (left == _float_id && right == _int_id)) {
  2890. return true;
  2891. }
  2892. if ((left == _uint_id && right == _float_id) || (left == _float_id && right == _uint_id)) {
  2893. return true;
  2894. }
  2895. if ((left == _uint_id && right == _int_id) || (left == _int_id && right == _uint_id)) {
  2896. return true;
  2897. }
  2898. return false;
  2899. }
  2900. static type_ref upgrade_type(type_ref left_type, type_ref right_type) {
  2901. type_id left = left_type.type;
  2902. type_id right = right_type.type;
  2903. if (left == right) {
  2904. return left_type;
  2905. }
  2906. if (left == _int_id && right == _float_id) {
  2907. return right_type;
  2908. }
  2909. if (left == _float_id && right == _int_id) {
  2910. return left_type;
  2911. }
  2912. if (left == _uint_id && right == _float_id) {
  2913. return right_type;
  2914. }
  2915. if (left == _float_id && right == _uint_id) {
  2916. return left_type;
  2917. }
  2918. if (left == _uint_id && right == _int_id) {
  2919. return right_type;
  2920. }
  2921. if (left == _int_id && right == _uint_id) {
  2922. return left_type;
  2923. }
  2924. kong_log(LOG_LEVEL_WARNING, "Suspicious type upgrade");
  2925. return left_type;
  2926. }
  2927. static void resolve_types_in_expression(statement *parent, expression *e) {
  2928. switch (e->kind) {
  2929. case EXPRESSION_BINARY: {
  2930. resolve_types_in_expression(parent, e->binary.left);
  2931. resolve_types_in_expression(parent, e->binary.right);
  2932. switch (e->binary.op) {
  2933. case OPERATOR_EQUALS:
  2934. case OPERATOR_NOT_EQUALS:
  2935. case OPERATOR_GREATER:
  2936. case OPERATOR_GREATER_EQUAL:
  2937. case OPERATOR_LESS:
  2938. case OPERATOR_LESS_EQUAL:
  2939. case OPERATOR_OR:
  2940. case OPERATOR_AND: {
  2941. e->type.type = _bool_id;
  2942. break;
  2943. }
  2944. case OPERATOR_BITWISE_XOR:
  2945. case OPERATOR_BITWISE_AND:
  2946. case OPERATOR_BITWISE_OR:
  2947. case OPERATOR_LEFT_SHIFT:
  2948. case OPERATOR_RIGHT_SHIFT: {
  2949. e->type = e->binary.left->type;
  2950. break;
  2951. }
  2952. case OPERATOR_MULTIPLY:
  2953. case OPERATOR_MULTIPLY_ASSIGN: {
  2954. type_id left_type = e->binary.left->type.type;
  2955. type_id right_type = e->binary.right->type.type;
  2956. if (types_compatible(left_type, right_type)) {
  2957. e->type = upgrade_type(e->binary.left->type, e->binary.right->type);
  2958. }
  2959. else {
  2960. debug_context context = {0};
  2961. error(context, "Type mismatch %s vs %s", _get_name(get_type(left_type)->name), _get_name(get_type(right_type)->name));
  2962. }
  2963. break;
  2964. }
  2965. case OPERATOR_MINUS:
  2966. case OPERATOR_PLUS:
  2967. case OPERATOR_DIVIDE:
  2968. case OPERATOR_MOD: {
  2969. type_id left_type = e->binary.left->type.type;
  2970. type_id right_type = e->binary.right->type.type;
  2971. if (!types_compatible(left_type, right_type)) {
  2972. debug_context context = {0};
  2973. error(context, "Type mismatch %s vs %s", _get_name(get_type(left_type)->name), _get_name(get_type(right_type)->name));
  2974. }
  2975. e->type = upgrade_type(e->binary.left->type, e->binary.right->type);
  2976. break;
  2977. }
  2978. case OPERATOR_ASSIGN:
  2979. case OPERATOR_DIVIDE_ASSIGN:
  2980. case OPERATOR_MINUS_ASSIGN:
  2981. case OPERATOR_PLUS_ASSIGN: {
  2982. type_id left_type = e->binary.left->type.type;
  2983. type_id right_type = e->binary.right->type.type;
  2984. if (!types_compatible(left_type, right_type)) {
  2985. debug_context context = {0};
  2986. error(context, "Type mismatch %s vs %s", _get_name(get_type(left_type)->name), _get_name(get_type(right_type)->name));
  2987. }
  2988. e->type = e->binary.left->type;
  2989. break;
  2990. }
  2991. case OPERATOR_NOT: {
  2992. debug_context context = {0};
  2993. error(context, "Weird binary operator");
  2994. break;
  2995. }
  2996. }
  2997. break;
  2998. }
  2999. case EXPRESSION_UNARY: {
  3000. resolve_types_in_expression(parent, e->unary.right);
  3001. switch (e->unary.op) {
  3002. case OPERATOR_MINUS:
  3003. case OPERATOR_PLUS: {
  3004. e->type = e->unary.right->type;
  3005. break;
  3006. }
  3007. case OPERATOR_NOT: {
  3008. e->type.type = _bool_id;
  3009. break;
  3010. }
  3011. case OPERATOR_EQUALS:
  3012. case OPERATOR_NOT_EQUALS:
  3013. case OPERATOR_GREATER:
  3014. case OPERATOR_GREATER_EQUAL:
  3015. case OPERATOR_LESS:
  3016. case OPERATOR_LESS_EQUAL:
  3017. case OPERATOR_DIVIDE:
  3018. case OPERATOR_MULTIPLY:
  3019. case OPERATOR_OR:
  3020. case OPERATOR_BITWISE_XOR:
  3021. case OPERATOR_BITWISE_AND:
  3022. case OPERATOR_BITWISE_OR:
  3023. case OPERATOR_LEFT_SHIFT:
  3024. case OPERATOR_RIGHT_SHIFT:
  3025. case OPERATOR_AND:
  3026. case OPERATOR_MOD:
  3027. case OPERATOR_ASSIGN:
  3028. default: {
  3029. debug_context context = {0};
  3030. error(context, "Weird unary operator");
  3031. break;
  3032. }
  3033. }
  3034. break;
  3035. }
  3036. case EXPRESSION_BOOLEAN: {
  3037. e->type.type = _bool_id;
  3038. break;
  3039. }
  3040. case EXPRESSION_FLOAT: {
  3041. e->type.type = _float_id;
  3042. break;
  3043. }
  3044. case EXPRESSION_INT: {
  3045. e->type.type = _int_id;
  3046. break;
  3047. }
  3048. case EXPRESSION_VARIABLE: {
  3049. global *g = find_global(e->variable);
  3050. if (g != NULL && g->type != NO_TYPE) {
  3051. e->type.type = g->type;
  3052. }
  3053. else {
  3054. type_ref type = find_local_var_type(&parent->block, e->variable);
  3055. if (type.type == NO_TYPE) {
  3056. type = find_local_var_type(&parent->block, e->variable);
  3057. debug_context context = {0};
  3058. error(context, "Variable %s not found", _get_name(e->variable));
  3059. }
  3060. e->type = type;
  3061. }
  3062. break;
  3063. }
  3064. case EXPRESSION_GROUPING: {
  3065. resolve_types_in_expression(parent, e->grouping);
  3066. e->type = e->grouping->type;
  3067. break;
  3068. }
  3069. case EXPRESSION_CALL: {
  3070. for (function_id i = 0; _get_function(i) != NULL; ++i) {
  3071. function *f = _get_function(i);
  3072. if (f->name == e->call.func_name) {
  3073. e->type = f->return_type;
  3074. break;
  3075. }
  3076. }
  3077. for (size_t i = 0; i < e->call.parameters.size; ++i) {
  3078. resolve_types_in_expression(parent, e->call.parameters.e[i]);
  3079. }
  3080. break;
  3081. }
  3082. case EXPRESSION_MEMBER: {
  3083. resolve_types_in_member(parent, e);
  3084. break;
  3085. }
  3086. case EXPRESSION_ELEMENT: {
  3087. resolve_types_in_element(parent, e);
  3088. break;
  3089. }
  3090. }
  3091. if (e->type.type == NO_TYPE) {
  3092. debug_context context = {0};
  3093. // const char *n = _get_name(e->call.func_name);
  3094. error(context, "Could not resolve type");
  3095. }
  3096. }
  3097. static void resolve_types_in_block(statement *parent, statement *block) {
  3098. debug_context context = {0};
  3099. check(block->kind == STATEMENT_BLOCK, context, "Malformed block");
  3100. for (size_t i = 0; i < block->block.statements.size; ++i) {
  3101. statement *s = block->block.statements.s[i];
  3102. switch (s->kind) {
  3103. case STATEMENT_EXPRESSION: {
  3104. resolve_types_in_expression(block, s->expression);
  3105. break;
  3106. }
  3107. case STATEMENT_RETURN_EXPRESSION: {
  3108. resolve_types_in_expression(block, s->expression);
  3109. break;
  3110. }
  3111. case STATEMENT_IF: {
  3112. resolve_types_in_expression(block, s->iffy.test);
  3113. resolve_types_in_block(block, s->iffy.if_block);
  3114. for (uint16_t i = 0; i < s->iffy.else_size; ++i) {
  3115. if (s->iffy.else_tests[i] != NULL) {
  3116. resolve_types_in_expression(block, s->iffy.else_tests[i]);
  3117. }
  3118. resolve_types_in_block(block, s->iffy.else_blocks[i]);
  3119. }
  3120. break;
  3121. }
  3122. case STATEMENT_WHILE:
  3123. case STATEMENT_DO_WHILE: {
  3124. resolve_types_in_expression(block, s->whiley.test);
  3125. resolve_types_in_block(block, s->whiley.while_block);
  3126. break;
  3127. }
  3128. case STATEMENT_BLOCK: {
  3129. resolve_types_in_block(block, s);
  3130. break;
  3131. }
  3132. case STATEMENT_LOCAL_VARIABLE: {
  3133. name_id var_name = s->local_variable.var.name;
  3134. name_id var_type_name = s->local_variable.var.type.unresolved.name;
  3135. if (s->local_variable.var.type.type == NO_TYPE && var_type_name != NO_NAME) {
  3136. s->local_variable.var.type.type = find_type_by_ref(&s->local_variable.var.type);
  3137. }
  3138. if (s->local_variable.var.type.type == NO_TYPE) {
  3139. debug_context context = {0};
  3140. error(context, "Could not find type %s for %s", _get_name(var_type_name), _get_name(var_name));
  3141. }
  3142. if (s->local_variable.init != NULL) {
  3143. resolve_types_in_expression(block, s->local_variable.init);
  3144. }
  3145. block->block.vars.v[block->block.vars.size].name = var_name;
  3146. block->block.vars.v[block->block.vars.size].type = s->local_variable.var.type;
  3147. ++block->block.vars.size;
  3148. break;
  3149. }
  3150. }
  3151. }
  3152. }
  3153. void _resolve_types(void) {
  3154. for (type_id i = 0; get_type(i) != NULL; ++i) {
  3155. type *s = get_type(i);
  3156. for (size_t j = 0; j < s->members.size; ++j) {
  3157. if (s->members.m[j].type.type == NO_TYPE) {
  3158. name_id name = s->members.m[j].type.unresolved.name;
  3159. s->members.m[j].type.type = find_type_by_name(name);
  3160. if (s->members.m[j].type.type == NO_TYPE) {
  3161. debug_context context = {0};
  3162. error(context, "Could not find type %s in %s", _get_name(name), _get_name(s->name));
  3163. }
  3164. }
  3165. }
  3166. }
  3167. for (function_id i = 0; _get_function(i) != NULL; ++i) {
  3168. function *f = _get_function(i);
  3169. for (uint8_t parameter_index = 0; parameter_index < f->parameters_size; ++parameter_index) {
  3170. if (f->parameter_types[parameter_index].type == NO_TYPE) {
  3171. name_id parameter_type_name = f->parameter_types[parameter_index].unresolved.name;
  3172. f->parameter_types[parameter_index].type = find_type_by_name(parameter_type_name);
  3173. if (f->parameter_types[parameter_index].type == NO_TYPE) {
  3174. debug_context context = {0};
  3175. error(context, "Could not find type %s for %s", _get_name(parameter_type_name), _get_name(f->name));
  3176. }
  3177. }
  3178. }
  3179. if (f->return_type.type == NO_TYPE) {
  3180. f->return_type.type = find_type_by_ref(&f->return_type);
  3181. if (f->return_type.type == NO_TYPE) {
  3182. error_no_context("Could not find type %s for %s", _get_name(f->return_type.unresolved.name), _get_name(f->name));
  3183. }
  3184. }
  3185. }
  3186. for (function_id i = 0; _get_function(i) != NULL; ++i) {
  3187. function *f = _get_function(i);
  3188. if (f->block == NULL) {
  3189. // built in
  3190. continue;
  3191. }
  3192. for (uint8_t parameter_index = 0; parameter_index < f->parameters_size; ++parameter_index) {
  3193. f->block->block.vars.v[f->block->block.vars.size].name = f->parameter_names[parameter_index];
  3194. f->block->block.vars.v[f->block->block.vars.size].type = f->parameter_types[parameter_index];
  3195. f->block->block.vars.v[f->block->block.vars.size].variable_id = 0;
  3196. ++f->block->block.vars.size;
  3197. }
  3198. resolve_types_in_block(NULL, f->block);
  3199. }
  3200. }
  3201. static void init_type_ref(type_ref *t, name_id name) {
  3202. t->type = NO_TYPE;
  3203. t->unresolved.name = name;
  3204. t->unresolved.array_size = 0;
  3205. }
  3206. void _types_init(void) {
  3207. type *new_types = realloc(types, types_size * sizeof(type));
  3208. debug_context context = {0};
  3209. check(new_types != NULL, context, "Could not allocate types");
  3210. types = new_types;
  3211. next_type_index = 0;
  3212. _void_id = add_type(add_name("void"));
  3213. get_type(_void_id)->built_in = true;
  3214. _bool_id = add_type(add_name("bool"));
  3215. get_type(_bool_id)->built_in = true;
  3216. _float_id = add_type(add_name("float"));
  3217. get_type(_float_id)->built_in = true;
  3218. _int_id = add_type(add_name("int"));
  3219. get_type(_int_id)->built_in = true;
  3220. _uint_id = add_type(add_name("uint"));
  3221. get_type(_uint_id)->built_in = true;
  3222. {
  3223. function_type_id = add_type(add_name("fun"));
  3224. get_type(function_type_id)->built_in = true;
  3225. }
  3226. }
  3227. static void grow_types_if_needed(uint64_t size) {
  3228. while (size >= types_size) {
  3229. types_size *= 2;
  3230. type *new_types = realloc(types, types_size * sizeof(type));
  3231. debug_context context = {0};
  3232. check(new_types != NULL, context, "Could not allocate types");
  3233. types = new_types;
  3234. }
  3235. }
  3236. static bool types_equal(type *a, type *b) {
  3237. return a->name == b->name && a->attributes.attributes_count == 0 && b->attributes.attributes_count == 0 && a->members.size == 0 && b->members.size == 0 &&
  3238. a->built_in == b->built_in && a->array_size == b->array_size && a->base == b->base;
  3239. }
  3240. static type_id add_type(name_id name) {
  3241. grow_types_if_needed(next_type_index + 1);
  3242. type_id s = next_type_index;
  3243. ++next_type_index;
  3244. types[s].name = name;
  3245. types[s].attributes.attributes_count = 0;
  3246. types[s].members.size = 0;
  3247. types[s].built_in = false;
  3248. types[s].array_size = 0;
  3249. types[s].base = NO_TYPE;
  3250. return s;
  3251. }
  3252. static type_id add_full_type(type *t) {
  3253. for (type_id type_index = 0; type_index < next_type_index; ++type_index) {
  3254. if (types_equal(&types[type_index], t)) {
  3255. return type_index;
  3256. }
  3257. }
  3258. grow_types_if_needed(next_type_index + 1);
  3259. type_id s = next_type_index;
  3260. ++next_type_index;
  3261. types[s] = *t;
  3262. return s;
  3263. }
  3264. static type_id find_type_by_name(name_id name) {
  3265. debug_context context = {0};
  3266. check(name != NO_NAME, context, "Attempted to find a no-name");
  3267. for (type_id i = 0; i < next_type_index; ++i) {
  3268. if (types[i].name == name) {
  3269. return i;
  3270. }
  3271. }
  3272. return NO_TYPE;
  3273. }
  3274. static type_id find_type_by_ref(type_ref *t) {
  3275. if (t->type != NO_TYPE) {
  3276. return t->type;
  3277. }
  3278. debug_context context = {0};
  3279. check(t->unresolved.name != NO_NAME, context, "Attempted to find a no-name");
  3280. type_id base_type_id = NO_TYPE;
  3281. for (type_id i = 0; i < next_type_index; ++i) {
  3282. if (types[i].name == t->unresolved.name) {
  3283. if (types[i].array_size == t->unresolved.array_size) {
  3284. return i;
  3285. }
  3286. base_type_id = i;
  3287. }
  3288. }
  3289. if (base_type_id != NO_TYPE) {
  3290. type_id new_type_id = add_type(t->unresolved.name);
  3291. type *new_type = get_type(new_type_id);
  3292. new_type->array_size = t->unresolved.array_size;
  3293. type *base_type = get_type(base_type_id);
  3294. new_type->base = base_type_id;
  3295. new_type->built_in = base_type->built_in;
  3296. return new_type_id;
  3297. }
  3298. return NO_TYPE;
  3299. }
  3300. static type *get_type(type_id s) {
  3301. if (s >= next_type_index) {
  3302. return NULL;
  3303. }
  3304. return &types[s];
  3305. }