parser.cpp 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798
  1. struct AstNode;
  2. enum ParseFileError {
  3. ParseFile_None,
  4. ParseFile_WrongExtension,
  5. ParseFile_InvalidFile,
  6. ParseFile_EmptyFile,
  7. ParseFile_Permission,
  8. ParseFile_NotFound,
  9. ParseFile_InvalidToken,
  10. ParseFile_Count,
  11. };
  12. struct AstFile {
  13. gbArena arena;
  14. Tokenizer tokenizer;
  15. gbArray(Token) tokens;
  16. Token * cursor; // NOTE(bill): Current token, easy to peek forward and backwards if needed
  17. // >= 0: In Expression
  18. // < 0: In Control Clause
  19. // NOTE(bill): Used to prevent type literals in control clauses
  20. isize expr_level;
  21. AstNode *decls;
  22. isize decl_count;
  23. AstNode *curr_proc;
  24. isize scope_level;
  25. ErrorCollector error_collector;
  26. // TODO(bill): Error recovery
  27. // NOTE(bill): Error recovery
  28. #define PARSER_MAX_FIX_COUNT 6
  29. isize fix_count;
  30. TokenPos fix_prev_pos;
  31. };
  32. struct Parser {
  33. String init_fullpath;
  34. gbArray(AstFile) files;
  35. gbArray(String) loads;
  36. gbArray(String) libraries;
  37. gbArray(String) system_libraries;
  38. isize load_index;
  39. isize total_token_count;
  40. };
  41. enum DeclKind {
  42. Declaration_Invalid,
  43. Declaration_Mutable,
  44. Declaration_Immutable,
  45. Declaration_Count,
  46. };
  47. enum ProcTag {
  48. ProcTag_foreign = GB_BIT(0),
  49. ProcTag_inline = GB_BIT(1),
  50. ProcTag_no_inline = GB_BIT(2),
  51. };
  52. enum VarDeclTag {
  53. VarDeclTag_thread_local = GB_BIT(0),
  54. };
  55. enum CallExprKind {
  56. CallExpr_Prefix, // call(...)
  57. CallExpr_Postfix, // a'call
  58. CallExpr_Infix, // a ''call b
  59. };
  60. #define AST_NODE_KINDS \
  61. AST_NODE_KIND(Invalid, "invalid node", struct{}) \
  62. AST_NODE_KIND(BasicLit, "basic literal", Token) \
  63. AST_NODE_KIND(Ident, "identifier", Token) \
  64. AST_NODE_KIND(Ellipsis, "ellipsis", struct { \
  65. Token token; \
  66. AstNode *expr; \
  67. }) \
  68. AST_NODE_KIND(ProcLit, "procedure literal", struct { \
  69. AstNode *type; \
  70. AstNode *body; \
  71. u64 tags; \
  72. }) \
  73. AST_NODE_KIND(CompoundLit, "compound literal", struct { \
  74. AstNode *type; \
  75. AstNode *elem_list; \
  76. isize elem_count; \
  77. Token open, close; \
  78. }) \
  79. AST_NODE_KIND(_ExprBegin, "", struct{}) \
  80. AST_NODE_KIND(BadExpr, "bad expression", struct { Token begin, end; }) \
  81. AST_NODE_KIND(TagExpr, "tag expression", struct { Token token, name; AstNode *expr; }) \
  82. AST_NODE_KIND(UnaryExpr, "unary expression", struct { Token op; AstNode *expr; }) \
  83. AST_NODE_KIND(BinaryExpr, "binary expression", struct { Token op; AstNode *left, *right; } ) \
  84. AST_NODE_KIND(ParenExpr, "parentheses expression", struct { AstNode *expr; Token open, close; }) \
  85. AST_NODE_KIND(SelectorExpr, "selector expression", struct { Token token; AstNode *expr, *selector; }) \
  86. AST_NODE_KIND(IndexExpr, "index expression", struct { AstNode *expr, *index; Token open, close; }) \
  87. AST_NODE_KIND(DerefExpr, "dereference expression", struct { Token op; AstNode *expr; }) \
  88. AST_NODE_KIND(CallExpr, "call expression", struct { \
  89. AstNode *proc, *arg_list; \
  90. isize arg_list_count; \
  91. Token open, close; \
  92. CallExprKind kind; \
  93. }) \
  94. AST_NODE_KIND(SliceExpr, "slice expression", struct { \
  95. AstNode *expr; \
  96. Token open, close; \
  97. AstNode *low, *high, *max; \
  98. b32 triple_indexed; \
  99. }) \
  100. AST_NODE_KIND(FieldValue, "field value", struct { Token eq; AstNode *field, *value; }) \
  101. AST_NODE_KIND(_ExprEnd, "", struct{}) \
  102. AST_NODE_KIND(_StmtBegin, "", struct{}) \
  103. AST_NODE_KIND(BadStmt, "bad statement", struct { Token begin, end; }) \
  104. AST_NODE_KIND(EmptyStmt, "empty statement", struct { Token token; }) \
  105. AST_NODE_KIND(ExprStmt, "expression statement", struct { AstNode *expr; } ) \
  106. AST_NODE_KIND(IncDecStmt, "increment/decrement statement", struct { Token op; AstNode *expr; }) \
  107. AST_NODE_KIND(TagStmt, "tag statement", struct { \
  108. Token token; \
  109. Token name; \
  110. AstNode *stmt; \
  111. }) \
  112. AST_NODE_KIND(AssignStmt, "assign statement", struct { \
  113. Token op; \
  114. AstNode *lhs_list, *rhs_list; \
  115. isize lhs_count, rhs_count; \
  116. }) \
  117. AST_NODE_KIND(_ComplexStmtBegin, "", struct{}) \
  118. AST_NODE_KIND(BlockStmt, "block statement", struct { \
  119. AstNode *list; \
  120. isize list_count; \
  121. Token open, close; \
  122. }) \
  123. AST_NODE_KIND(IfStmt, "if statement", struct { \
  124. Token token; \
  125. AstNode *init; \
  126. AstNode *cond; \
  127. AstNode *body; \
  128. AstNode *else_stmt; \
  129. }) \
  130. AST_NODE_KIND(ReturnStmt, "return statement", struct { \
  131. Token token; \
  132. AstNode *result_list; \
  133. isize result_count; \
  134. }) \
  135. AST_NODE_KIND(ForStmt, "for statement", struct { \
  136. Token token; \
  137. AstNode *init, *cond, *post; \
  138. AstNode *body; \
  139. }) \
  140. AST_NODE_KIND(CaseClause, "case clause", struct { \
  141. Token token; \
  142. AstNode *list; \
  143. AstNode *stmts; \
  144. isize list_count, stmt_count; \
  145. }) \
  146. AST_NODE_KIND(MatchStmt, "match statement", struct { \
  147. Token token; \
  148. AstNode *init, *tag; \
  149. AstNode *body; \
  150. }) \
  151. AST_NODE_KIND(TypeMatchStmt, "type match statement", struct { \
  152. Token token; \
  153. AstNode *tag, *var; \
  154. AstNode *body; \
  155. }) \
  156. AST_NODE_KIND(DeferStmt, "defer statement", struct { Token token; AstNode *stmt; }) \
  157. AST_NODE_KIND(BranchStmt, "branch statement", struct { Token token; }) \
  158. AST_NODE_KIND(UsingStmt, "using statement", struct { Token token; AstNode *node; }) \
  159. AST_NODE_KIND(AsmOperand, "assembly operand", struct { \
  160. Token string; \
  161. AstNode *operand; \
  162. }) \
  163. AST_NODE_KIND(AsmStmt, "assembly statement", struct { \
  164. Token token; \
  165. b32 is_volatile; \
  166. Token open, close; \
  167. Token code_string; \
  168. AstNode *output_list; \
  169. AstNode *input_list; \
  170. AstNode *clobber_list; \
  171. isize output_count, input_count, clobber_count; \
  172. }) \
  173. \
  174. AST_NODE_KIND(_ComplexStmtEnd, "", struct{}) \
  175. AST_NODE_KIND(_StmtEnd, "", struct{}) \
  176. AST_NODE_KIND(_DeclBegin, "", struct{}) \
  177. AST_NODE_KIND(BadDecl, "bad declaration", struct { Token begin, end; }) \
  178. AST_NODE_KIND(VarDecl, "variable declaration", struct { \
  179. DeclKind kind; \
  180. u32 tags; \
  181. b32 is_using; \
  182. AstNode *name_list; \
  183. AstNode *type; \
  184. AstNode *value_list; \
  185. isize name_count, value_count; \
  186. }) \
  187. AST_NODE_KIND(ProcDecl, "procedure declaration", struct { \
  188. AstNode *name; \
  189. AstNode *type; \
  190. AstNode *body; \
  191. u64 tags; \
  192. String foreign_name; \
  193. }) \
  194. AST_NODE_KIND(TypeDecl, "type declaration", struct { Token token; AstNode *name, *type; }) \
  195. AST_NODE_KIND(LoadDecl, "load declaration", struct { Token token, filepath; }) \
  196. AST_NODE_KIND(ForeignSystemLibrary, "foreign system library", struct { Token token, filepath; }) \
  197. AST_NODE_KIND(_DeclEnd, "", struct{}) \
  198. AST_NODE_KIND(_TypeBegin, "", struct{}) \
  199. AST_NODE_KIND(Field, "field", struct { \
  200. AstNode *name_list; \
  201. isize name_count; \
  202. AstNode *type; \
  203. b32 is_using; \
  204. }) \
  205. AST_NODE_KIND(ProcType, "procedure type", struct { \
  206. Token token; \
  207. AstNode *param_list; \
  208. AstNode *result_list; \
  209. isize param_count; \
  210. isize result_count; \
  211. }) \
  212. AST_NODE_KIND(PointerType, "pointer type", struct { \
  213. Token token; \
  214. AstNode *type; \
  215. }) \
  216. AST_NODE_KIND(ArrayType, "array type", struct { \
  217. Token token; \
  218. AstNode *count; \
  219. AstNode *elem; \
  220. }) \
  221. AST_NODE_KIND(VectorType, "vector type", struct { \
  222. Token token; \
  223. AstNode *count; \
  224. AstNode *elem; \
  225. }) \
  226. AST_NODE_KIND(StructType, "struct type", struct { \
  227. Token token; \
  228. AstNode *decl_list; \
  229. isize decl_count; \
  230. b32 is_packed; \
  231. }) \
  232. AST_NODE_KIND(UnionType, "union type", struct { \
  233. Token token; \
  234. AstNode *decl_list; \
  235. isize decl_count; \
  236. }) \
  237. AST_NODE_KIND(RawUnionType, "raw union type", struct { \
  238. Token token; \
  239. AstNode *decl_list; \
  240. isize decl_count; \
  241. }) \
  242. AST_NODE_KIND(EnumType, "enum type", struct { \
  243. Token token; \
  244. AstNode *base_type; \
  245. AstNode *field_list; \
  246. isize field_count; \
  247. }) \
  248. AST_NODE_KIND(_TypeEnd, "", struct{}) \
  249. AST_NODE_KIND(Count, "", struct{})
  250. enum AstNodeKind {
  251. #define AST_NODE_KIND(_kind_name_, ...) GB_JOIN2(AstNode_, _kind_name_),
  252. AST_NODE_KINDS
  253. #undef AST_NODE_KIND
  254. };
  255. String const ast_node_strings[] = {
  256. #define AST_NODE_KIND(_kind_name_, name, ...) {cast(u8 *)name, gb_size_of(name)-1},
  257. AST_NODE_KINDS
  258. #undef AST_NODE_KIND
  259. };
  260. struct AstNode {
  261. AstNodeKind kind;
  262. AstNode *prev, *next; // NOTE(bill): allow for Linked list
  263. union {
  264. #define AST_NODE_KIND(_kind_name_, name, ...) __VA_ARGS__ _kind_name_;
  265. AST_NODE_KINDS
  266. #undef AST_NODE_KIND
  267. };
  268. };
  269. #define ast_node(n_, Kind_, node_) auto *n_ = &(node_)->Kind_; GB_ASSERT((node_)->kind == GB_JOIN2(AstNode_, Kind_))
  270. #define case_ast_node(n_, Kind_, node_) case GB_JOIN2(AstNode_, Kind_): { ast_node(n_, Kind_, node_);
  271. #define case_end } break;
  272. gb_inline b32 is_ast_node_expr(AstNode *node) {
  273. return gb_is_between(node->kind, AstNode__ExprBegin+1, AstNode__ExprEnd-1);
  274. }
  275. gb_inline b32 is_ast_node_stmt(AstNode *node) {
  276. return gb_is_between(node->kind, AstNode__StmtBegin+1, AstNode__StmtEnd-1);
  277. }
  278. gb_inline b32 is_ast_node_complex_stmt(AstNode *node) {
  279. return gb_is_between(node->kind, AstNode__ComplexStmtBegin+1, AstNode__ComplexStmtEnd-1);
  280. }
  281. gb_inline b32 is_ast_node_decl(AstNode *node) {
  282. return gb_is_between(node->kind, AstNode__DeclBegin+1, AstNode__DeclEnd-1);
  283. }
  284. gb_inline b32 is_ast_node_type(AstNode *node) {
  285. return gb_is_between(node->kind, AstNode__TypeBegin+1, AstNode__TypeEnd-1);
  286. }
  287. Token ast_node_token(AstNode *node) {
  288. switch (node->kind) {
  289. case AstNode_BasicLit:
  290. return node->BasicLit;
  291. case AstNode_Ident:
  292. return node->Ident;
  293. case AstNode_ProcLit:
  294. return ast_node_token(node->ProcLit.type);
  295. case AstNode_CompoundLit:
  296. return ast_node_token(node->CompoundLit.type);
  297. case AstNode_TagExpr:
  298. return node->TagExpr.token;
  299. case AstNode_BadExpr:
  300. return node->BadExpr.begin;
  301. case AstNode_UnaryExpr:
  302. return node->UnaryExpr.op;
  303. case AstNode_BinaryExpr:
  304. return ast_node_token(node->BinaryExpr.left);
  305. case AstNode_ParenExpr:
  306. return node->ParenExpr.open;
  307. case AstNode_CallExpr:
  308. return ast_node_token(node->CallExpr.proc);
  309. case AstNode_SelectorExpr:
  310. return ast_node_token(node->SelectorExpr.selector);
  311. case AstNode_IndexExpr:
  312. return node->IndexExpr.open;
  313. case AstNode_SliceExpr:
  314. return node->SliceExpr.open;
  315. case AstNode_Ellipsis:
  316. return node->Ellipsis.token;
  317. case AstNode_FieldValue:
  318. return node->FieldValue.eq;
  319. case AstNode_DerefExpr:
  320. return node->DerefExpr.op;
  321. case AstNode_BadStmt:
  322. return node->BadStmt.begin;
  323. case AstNode_EmptyStmt:
  324. return node->EmptyStmt.token;
  325. case AstNode_ExprStmt:
  326. return ast_node_token(node->ExprStmt.expr);
  327. case AstNode_TagStmt:
  328. return node->TagStmt.token;
  329. case AstNode_IncDecStmt:
  330. return node->IncDecStmt.op;
  331. case AstNode_AssignStmt:
  332. return node->AssignStmt.op;
  333. case AstNode_BlockStmt:
  334. return node->BlockStmt.open;
  335. case AstNode_IfStmt:
  336. return node->IfStmt.token;
  337. case AstNode_ReturnStmt:
  338. return node->ReturnStmt.token;
  339. case AstNode_ForStmt:
  340. return node->ForStmt.token;
  341. case AstNode_MatchStmt:
  342. return node->MatchStmt.token;
  343. case AstNode_CaseClause:
  344. return node->CaseClause.token;
  345. case AstNode_DeferStmt:
  346. return node->DeferStmt.token;
  347. case AstNode_BranchStmt:
  348. return node->BranchStmt.token;
  349. case AstNode_UsingStmt:
  350. return node->UsingStmt.token;
  351. case AstNode_AsmStmt:
  352. return node->AsmStmt.token;
  353. case AstNode_BadDecl:
  354. return node->BadDecl.begin;
  355. case AstNode_VarDecl:
  356. return ast_node_token(node->VarDecl.name_list);
  357. case AstNode_ProcDecl:
  358. return node->ProcDecl.name->Ident;
  359. case AstNode_TypeDecl:
  360. return node->TypeDecl.token;
  361. case AstNode_LoadDecl:
  362. return node->LoadDecl.token;
  363. case AstNode_ForeignSystemLibrary:
  364. return node->ForeignSystemLibrary.token;
  365. case AstNode_Field: {
  366. if (node->Field.name_list)
  367. return ast_node_token(node->Field.name_list);
  368. else
  369. return ast_node_token(node->Field.type);
  370. }
  371. case AstNode_ProcType:
  372. return node->ProcType.token;
  373. case AstNode_PointerType:
  374. return node->PointerType.token;
  375. case AstNode_ArrayType:
  376. return node->ArrayType.token;
  377. case AstNode_VectorType:
  378. return node->VectorType.token;
  379. case AstNode_StructType:
  380. return node->StructType.token;
  381. case AstNode_UnionType:
  382. return node->UnionType.token;
  383. case AstNode_RawUnionType:
  384. return node->RawUnionType.token;
  385. case AstNode_EnumType:
  386. return node->EnumType.token;
  387. }
  388. return empty_token;
  389. }
  390. HashKey hash_token(Token t) {
  391. return hash_string(t.string);
  392. }
  393. #define ast_file_err(f, token, fmt, ...) ast_file_err_(f, __FUNCTION__, token, fmt, ##__VA_ARGS__)
  394. void ast_file_err_(AstFile *file, char *function, Token token, char *fmt, ...) {
  395. // NOTE(bill): Duplicate error, skip it
  396. if (!token_pos_are_equal(file->error_collector.prev, token.pos)) {
  397. va_list va;
  398. file->error_collector.prev = token.pos;
  399. #if 0
  400. gb_printf_err("%s()\n", function);
  401. #endif
  402. va_start(va, fmt);
  403. gb_printf_err("%.*s(%td:%td) Syntax error: %s\n",
  404. LIT(token.pos.file), token.pos.line, token.pos.column,
  405. gb_bprintf_va(fmt, va));
  406. va_end(va);
  407. }
  408. file->error_collector.count++;
  409. }
  410. // NOTE(bill): And this below is why is I/we need a new language! Discriminated unions are a pain in C/C++
  411. gb_inline AstNode *make_node(AstFile *f, AstNodeKind kind) {
  412. gbArena *arena = &f->arena;
  413. if (gb_arena_size_remaining(arena, GB_DEFAULT_MEMORY_ALIGNMENT) <= gb_size_of(AstNode)) {
  414. // NOTE(bill): If a syntax error is so bad, just quit!
  415. gb_exit(1);
  416. }
  417. AstNode *node = gb_alloc_item(gb_arena_allocator(arena), AstNode);
  418. node->kind = kind;
  419. return node;
  420. }
  421. gb_inline AstNode *make_bad_expr(AstFile *f, Token begin, Token end) {
  422. AstNode *result = make_node(f, AstNode_BadExpr);
  423. result->BadExpr.begin = begin;
  424. result->BadExpr.end = end;
  425. return result;
  426. }
  427. gb_inline AstNode *make_tag_expr(AstFile *f, Token token, Token name, AstNode *expr) {
  428. AstNode *result = make_node(f, AstNode_TagExpr);
  429. result->TagExpr.token = token;
  430. result->TagExpr.name = name;
  431. result->TagExpr.expr = expr;
  432. return result;
  433. }
  434. gb_inline AstNode *make_tag_stmt(AstFile *f, Token token, Token name, AstNode *stmt) {
  435. AstNode *result = make_node(f, AstNode_TagStmt);
  436. result->TagStmt.token = token;
  437. result->TagStmt.name = name;
  438. result->TagStmt.stmt = stmt;
  439. return result;
  440. }
  441. gb_inline AstNode *make_unary_expr(AstFile *f, Token op, AstNode *expr) {
  442. AstNode *result = make_node(f, AstNode_UnaryExpr);
  443. result->UnaryExpr.op = op;
  444. result->UnaryExpr.expr = expr;
  445. return result;
  446. }
  447. gb_inline AstNode *make_binary_expr(AstFile *f, Token op, AstNode *left, AstNode *right) {
  448. AstNode *result = make_node(f, AstNode_BinaryExpr);
  449. if (left == NULL) {
  450. ast_file_err(f, op, "No lhs expression for binary expression `%.*s`", LIT(op.string));
  451. left = make_bad_expr(f, op, op);
  452. }
  453. if (right == NULL) {
  454. ast_file_err(f, op, "No rhs expression for binary expression `%.*s`", LIT(op.string));
  455. right = make_bad_expr(f, op, op);
  456. }
  457. result->BinaryExpr.op = op;
  458. result->BinaryExpr.left = left;
  459. result->BinaryExpr.right = right;
  460. return result;
  461. }
  462. gb_inline AstNode *make_paren_expr(AstFile *f, AstNode *expr, Token open, Token close) {
  463. AstNode *result = make_node(f, AstNode_ParenExpr);
  464. result->ParenExpr.expr = expr;
  465. result->ParenExpr.open = open;
  466. result->ParenExpr.close = close;
  467. return result;
  468. }
  469. gb_inline AstNode *make_call_expr(AstFile *f, AstNode *proc, AstNode *arg_list, isize arg_list_count, Token open, Token close) {
  470. AstNode *result = make_node(f, AstNode_CallExpr);
  471. result->CallExpr.proc = proc;
  472. result->CallExpr.arg_list = arg_list;
  473. result->CallExpr.arg_list_count = arg_list_count;
  474. result->CallExpr.open = open;
  475. result->CallExpr.close = close;
  476. return result;
  477. }
  478. gb_inline AstNode *make_selector_expr(AstFile *f, Token token, AstNode *expr, AstNode *selector) {
  479. AstNode *result = make_node(f, AstNode_SelectorExpr);
  480. result->SelectorExpr.expr = expr;
  481. result->SelectorExpr.selector = selector;
  482. return result;
  483. }
  484. gb_inline AstNode *make_index_expr(AstFile *f, AstNode *expr, AstNode *index, Token open, Token close) {
  485. AstNode *result = make_node(f, AstNode_IndexExpr);
  486. result->IndexExpr.expr = expr;
  487. result->IndexExpr.index = index;
  488. result->IndexExpr.open = open;
  489. result->IndexExpr.close = close;
  490. return result;
  491. }
  492. gb_inline AstNode *make_slice_expr(AstFile *f, AstNode *expr, Token open, Token close, AstNode *low, AstNode *high, AstNode *max, b32 triple_indexed) {
  493. AstNode *result = make_node(f, AstNode_SliceExpr);
  494. result->SliceExpr.expr = expr;
  495. result->SliceExpr.open = open;
  496. result->SliceExpr.close = close;
  497. result->SliceExpr.low = low;
  498. result->SliceExpr.high = high;
  499. result->SliceExpr.max = max;
  500. result->SliceExpr.triple_indexed = triple_indexed;
  501. return result;
  502. }
  503. gb_inline AstNode *make_deref_expr(AstFile *f, AstNode *expr, Token op) {
  504. AstNode *result = make_node(f, AstNode_DerefExpr);
  505. result->DerefExpr.expr = expr;
  506. result->DerefExpr.op = op;
  507. return result;
  508. }
  509. gb_inline AstNode *make_basic_lit(AstFile *f, Token basic_lit) {
  510. AstNode *result = make_node(f, AstNode_BasicLit);
  511. result->BasicLit = basic_lit;
  512. return result;
  513. }
  514. gb_inline AstNode *make_ident(AstFile *f, Token token) {
  515. AstNode *result = make_node(f, AstNode_Ident);
  516. result->Ident = token;
  517. return result;
  518. }
  519. gb_inline AstNode *make_ellipsis(AstFile *f, Token token, AstNode *expr) {
  520. AstNode *result = make_node(f, AstNode_Ellipsis);
  521. result->Ellipsis.token = token;
  522. result->Ellipsis.expr = expr;
  523. return result;
  524. }
  525. gb_inline AstNode *make_proc_lit(AstFile *f, AstNode *type, AstNode *body, u64 tags) {
  526. AstNode *result = make_node(f, AstNode_ProcLit);
  527. result->ProcLit.type = type;
  528. result->ProcLit.body = body;
  529. result->ProcLit.tags = tags;
  530. return result;
  531. }
  532. gb_inline AstNode *make_field_value(AstFile *f, AstNode *field, AstNode *value, Token eq) {
  533. AstNode *result = make_node(f, AstNode_FieldValue);
  534. result->FieldValue.field = field;
  535. result->FieldValue.value = value;
  536. result->FieldValue.eq = eq;
  537. return result;
  538. }
  539. gb_inline AstNode *make_compound_lit(AstFile *f, AstNode *type, AstNode *elem_list, isize elem_count,
  540. Token open, Token close) {
  541. AstNode *result = make_node(f, AstNode_CompoundLit);
  542. result->CompoundLit.type = type;
  543. result->CompoundLit.elem_list = elem_list;
  544. result->CompoundLit.elem_count = elem_count;
  545. result->CompoundLit.open = open;
  546. result->CompoundLit.close = close;
  547. return result;
  548. }
  549. gb_inline AstNode *make_bad_stmt(AstFile *f, Token begin, Token end) {
  550. AstNode *result = make_node(f, AstNode_BadStmt);
  551. result->BadStmt.begin = begin;
  552. result->BadStmt.end = end;
  553. return result;
  554. }
  555. gb_inline AstNode *make_empty_stmt(AstFile *f, Token token) {
  556. AstNode *result = make_node(f, AstNode_EmptyStmt);
  557. result->EmptyStmt.token = token;
  558. return result;
  559. }
  560. gb_inline AstNode *make_expr_stmt(AstFile *f, AstNode *expr) {
  561. AstNode *result = make_node(f, AstNode_ExprStmt);
  562. result->ExprStmt.expr = expr;
  563. return result;
  564. }
  565. gb_inline AstNode *make_inc_dec_stmt(AstFile *f, Token op, AstNode *expr) {
  566. AstNode *result = make_node(f, AstNode_IncDecStmt);
  567. result->IncDecStmt.op = op;
  568. result->IncDecStmt.expr = expr;
  569. return result;
  570. }
  571. gb_inline AstNode *make_assign_stmt(AstFile *f, Token op, AstNode *lhs_list, isize lhs_count, AstNode *rhs_list, isize rhs_count) {
  572. AstNode *result = make_node(f, AstNode_AssignStmt);
  573. result->AssignStmt.op = op;
  574. result->AssignStmt.lhs_list = lhs_list;
  575. result->AssignStmt.lhs_count = lhs_count;
  576. result->AssignStmt.rhs_list = rhs_list;
  577. result->AssignStmt.rhs_count = rhs_count;
  578. return result;
  579. }
  580. gb_inline AstNode *make_block_stmt(AstFile *f, AstNode *list, isize list_count, Token open, Token close) {
  581. AstNode *result = make_node(f, AstNode_BlockStmt);
  582. result->BlockStmt.list = list;
  583. result->BlockStmt.list_count = list_count;
  584. result->BlockStmt.open = open;
  585. result->BlockStmt.close = close;
  586. return result;
  587. }
  588. gb_inline AstNode *make_if_stmt(AstFile *f, Token token, AstNode *init, AstNode *cond, AstNode *body, AstNode *else_stmt) {
  589. AstNode *result = make_node(f, AstNode_IfStmt);
  590. result->IfStmt.token = token;
  591. result->IfStmt.init = init;
  592. result->IfStmt.cond = cond;
  593. result->IfStmt.body = body;
  594. result->IfStmt.else_stmt = else_stmt;
  595. return result;
  596. }
  597. gb_inline AstNode *make_return_stmt(AstFile *f, Token token, AstNode *result_list, isize result_count) {
  598. AstNode *result = make_node(f, AstNode_ReturnStmt);
  599. result->ReturnStmt.token = token;
  600. result->ReturnStmt.result_list = result_list;
  601. result->ReturnStmt.result_count = result_count;
  602. return result;
  603. }
  604. gb_inline AstNode *make_for_stmt(AstFile *f, Token token, AstNode *init, AstNode *cond, AstNode *post, AstNode *body) {
  605. AstNode *result = make_node(f, AstNode_ForStmt);
  606. result->ForStmt.token = token;
  607. result->ForStmt.init = init;
  608. result->ForStmt.cond = cond;
  609. result->ForStmt.post = post;
  610. result->ForStmt.body = body;
  611. return result;
  612. }
  613. gb_inline AstNode *make_match_stmt(AstFile *f, Token token, AstNode *init, AstNode *tag, AstNode *body) {
  614. AstNode *result = make_node(f, AstNode_MatchStmt);
  615. result->MatchStmt.token = token;
  616. result->MatchStmt.init = init;
  617. result->MatchStmt.tag = tag;
  618. result->MatchStmt.body = body;
  619. return result;
  620. }
  621. gb_inline AstNode *make_type_match_stmt(AstFile *f, Token token, AstNode *tag, AstNode *var, AstNode *body) {
  622. AstNode *result = make_node(f, AstNode_TypeMatchStmt);
  623. result->TypeMatchStmt.token = token;
  624. result->TypeMatchStmt.tag = tag;
  625. result->TypeMatchStmt.var = var;
  626. result->TypeMatchStmt.body = body;
  627. return result;
  628. }
  629. gb_inline AstNode *make_case_clause(AstFile *f, Token token, AstNode *list, isize list_count, AstNode *stmts, isize stmt_count) {
  630. AstNode *result = make_node(f, AstNode_CaseClause);
  631. result->CaseClause.token = token;
  632. result->CaseClause.list = list;
  633. result->CaseClause.list_count = list_count;
  634. result->CaseClause.stmts = stmts;
  635. result->CaseClause.stmt_count = stmt_count;
  636. return result;
  637. }
  638. gb_inline AstNode *make_defer_stmt(AstFile *f, Token token, AstNode *stmt) {
  639. AstNode *result = make_node(f, AstNode_DeferStmt);
  640. result->DeferStmt.token = token;
  641. result->DeferStmt.stmt = stmt;
  642. return result;
  643. }
  644. gb_inline AstNode *make_branch_stmt(AstFile *f, Token token) {
  645. AstNode *result = make_node(f, AstNode_BranchStmt);
  646. result->BranchStmt.token = token;
  647. return result;
  648. }
  649. gb_inline AstNode *make_using_stmt(AstFile *f, Token token, AstNode *node) {
  650. AstNode *result = make_node(f, AstNode_UsingStmt);
  651. result->UsingStmt.token = token;
  652. result->UsingStmt.node = node;
  653. return result;
  654. }
  655. gb_inline AstNode *make_asm_operand(AstFile *f, Token string, AstNode *operand) {
  656. AstNode *result = make_node(f, AstNode_AsmOperand);
  657. result->AsmOperand.string = string;
  658. result->AsmOperand.operand = operand;
  659. return result;
  660. }
  661. gb_inline AstNode *make_asm_stmt(AstFile *f, Token token, b32 is_volatile, Token open, Token close, Token code_string,
  662. AstNode *output_list, AstNode *input_list, AstNode *clobber_list,
  663. isize output_count, isize input_count, isize clobber_count) {
  664. AstNode *result = make_node(f, AstNode_AsmStmt);
  665. result->AsmStmt.token = token;
  666. result->AsmStmt.is_volatile = is_volatile;
  667. result->AsmStmt.open = open;
  668. result->AsmStmt.close = close;
  669. result->AsmStmt.code_string = code_string;
  670. result->AsmStmt.output_list = output_list;
  671. result->AsmStmt.input_list = input_list;
  672. result->AsmStmt.clobber_list = clobber_list;
  673. result->AsmStmt.output_count = output_count;
  674. result->AsmStmt.input_count = input_count;
  675. result->AsmStmt.clobber_count = clobber_count;
  676. return result;
  677. }
  678. gb_inline AstNode *make_bad_decl(AstFile *f, Token begin, Token end) {
  679. AstNode *result = make_node(f, AstNode_BadDecl);
  680. result->BadDecl.begin = begin;
  681. result->BadDecl.end = end;
  682. return result;
  683. }
  684. gb_inline AstNode *make_var_decl(AstFile *f, DeclKind kind, AstNode *name_list, isize name_count, AstNode *type, AstNode *value_list, isize value_count) {
  685. AstNode *result = make_node(f, AstNode_VarDecl);
  686. result->VarDecl.kind = kind;
  687. result->VarDecl.name_list = name_list;
  688. result->VarDecl.name_count = name_count;
  689. result->VarDecl.type = type;
  690. result->VarDecl.value_list = value_list;
  691. result->VarDecl.value_count = value_count;
  692. return result;
  693. }
  694. gb_inline AstNode *make_field(AstFile *f, AstNode *name_list, isize name_count, AstNode *type, b32 is_using) {
  695. AstNode *result = make_node(f, AstNode_Field);
  696. result->Field.name_list = name_list;
  697. result->Field.name_count = name_count;
  698. result->Field.type = type;
  699. result->Field.is_using = is_using;
  700. return result;
  701. }
  702. gb_inline AstNode *make_proc_type(AstFile *f, Token token, AstNode *param_list, isize param_count, AstNode *result_list, isize result_count) {
  703. AstNode *result = make_node(f, AstNode_ProcType);
  704. result->ProcType.token = token;
  705. result->ProcType.param_list = param_list;
  706. result->ProcType.param_count = param_count;
  707. result->ProcType.result_list = result_list;
  708. result->ProcType.result_count = result_count;
  709. return result;
  710. }
  711. gb_inline AstNode *make_proc_decl(AstFile *f, AstNode *name, AstNode *proc_type, AstNode *body, u64 tags, String foreign_name) {
  712. AstNode *result = make_node(f, AstNode_ProcDecl);
  713. result->ProcDecl.name = name;
  714. result->ProcDecl.type = proc_type;
  715. result->ProcDecl.body = body;
  716. result->ProcDecl.tags = tags;
  717. result->ProcDecl.foreign_name = foreign_name;
  718. return result;
  719. }
  720. gb_inline AstNode *make_pointer_type(AstFile *f, Token token, AstNode *type) {
  721. AstNode *result = make_node(f, AstNode_PointerType);
  722. result->PointerType.token = token;
  723. result->PointerType.type = type;
  724. return result;
  725. }
  726. gb_inline AstNode *make_array_type(AstFile *f, Token token, AstNode *count, AstNode *elem) {
  727. AstNode *result = make_node(f, AstNode_ArrayType);
  728. result->ArrayType.token = token;
  729. result->ArrayType.count = count;
  730. result->ArrayType.elem = elem;
  731. return result;
  732. }
  733. gb_inline AstNode *make_vector_type(AstFile *f, Token token, AstNode *count, AstNode *elem) {
  734. AstNode *result = make_node(f, AstNode_VectorType);
  735. result->VectorType.token = token;
  736. result->VectorType.count = count;
  737. result->VectorType.elem = elem;
  738. return result;
  739. }
  740. gb_inline AstNode *make_struct_type(AstFile *f, Token token, AstNode *decl_list, isize decl_count, b32 is_packed) {
  741. AstNode *result = make_node(f, AstNode_StructType);
  742. result->StructType.token = token;
  743. result->StructType.decl_list = decl_list;
  744. result->StructType.decl_count = decl_count;
  745. result->StructType.is_packed = is_packed;
  746. return result;
  747. }
  748. gb_inline AstNode *make_union_type(AstFile *f, Token token, AstNode *decl_list, isize decl_count) {
  749. AstNode *result = make_node(f, AstNode_UnionType);
  750. result->UnionType.token = token;
  751. result->UnionType.decl_list = decl_list;
  752. result->UnionType.decl_count = decl_count;
  753. return result;
  754. }
  755. gb_inline AstNode *make_raw_union_type(AstFile *f, Token token, AstNode *decl_list, isize decl_count) {
  756. AstNode *result = make_node(f, AstNode_RawUnionType);
  757. result->RawUnionType.token = token;
  758. result->RawUnionType.decl_list = decl_list;
  759. result->RawUnionType.decl_count = decl_count;
  760. return result;
  761. }
  762. gb_inline AstNode *make_enum_type(AstFile *f, Token token, AstNode *base_type, AstNode *field_list, isize field_count) {
  763. AstNode *result = make_node(f, AstNode_EnumType);
  764. result->EnumType.token = token;
  765. result->EnumType.base_type = base_type;
  766. result->EnumType.field_list = field_list;
  767. result->EnumType.field_count = field_count;
  768. return result;
  769. }
  770. gb_inline AstNode *make_type_decl(AstFile *f, Token token, AstNode *name, AstNode *type) {
  771. AstNode *result = make_node(f, AstNode_TypeDecl);
  772. result->TypeDecl.token = token;
  773. result->TypeDecl.name = name;
  774. result->TypeDecl.type = type;
  775. return result;
  776. }
  777. gb_inline AstNode *make_load_decl(AstFile *f, Token token, Token filepath) {
  778. AstNode *result = make_node(f, AstNode_LoadDecl);
  779. result->LoadDecl.token = token;
  780. result->LoadDecl.filepath = filepath;
  781. return result;
  782. }
  783. gb_inline AstNode *make_foreign_system_library(AstFile *f, Token token, Token filepath) {
  784. AstNode *result = make_node(f, AstNode_ForeignSystemLibrary);
  785. result->ForeignSystemLibrary.token = token;
  786. result->ForeignSystemLibrary.filepath = filepath;
  787. return result;
  788. }
  789. gb_inline b32 next_token(AstFile *f) {
  790. if (f->cursor+1 < f->tokens + gb_array_count(f->tokens)) {
  791. f->cursor++;
  792. return true;
  793. } else {
  794. ast_file_err(f, f->cursor[0], "Token is EOF");
  795. return false;
  796. }
  797. }
  798. gb_inline Token expect_token(AstFile *f, TokenKind kind) {
  799. Token prev = f->cursor[0];
  800. if (prev.kind != kind) {
  801. ast_file_err(f, f->cursor[0], "Expected `%.*s`, got `%.*s`",
  802. LIT(token_strings[kind]),
  803. LIT(token_strings[prev.kind]));
  804. }
  805. next_token(f);
  806. return prev;
  807. }
  808. gb_inline Token expect_operator(AstFile *f) {
  809. Token prev = f->cursor[0];
  810. if (!gb_is_between(prev.kind, Token__OperatorBegin+1, Token__OperatorEnd-1)) {
  811. ast_file_err(f, f->cursor[0], "Expected an operator, got `%.*s`",
  812. LIT(token_strings[prev.kind]));
  813. }
  814. next_token(f);
  815. return prev;
  816. }
  817. gb_inline Token expect_keyword(AstFile *f) {
  818. Token prev = f->cursor[0];
  819. if (!gb_is_between(prev.kind, Token__KeywordBegin+1, Token__KeywordEnd-1)) {
  820. ast_file_err(f, f->cursor[0], "Expected a keyword, got `%.*s`",
  821. LIT(token_strings[prev.kind]));
  822. }
  823. next_token(f);
  824. return prev;
  825. }
  826. gb_inline b32 allow_token(AstFile *f, TokenKind kind) {
  827. Token prev = f->cursor[0];
  828. if (prev.kind == kind) {
  829. next_token(f);
  830. return true;
  831. }
  832. return false;
  833. }
  834. b32 is_blank_ident(String str) {
  835. if (str.len == 1) {
  836. return str.text[0] == '_';
  837. }
  838. return false;
  839. }
  840. void fix_advance_to_next_stmt(AstFile *f) {
  841. // TODO(bill): fix_advance_to_next_stmt
  842. #if 0
  843. for (;;) {
  844. Token t = f->cursor[0];
  845. switch (t.kind) {
  846. case Token_EOF:
  847. return;
  848. case Token_type:
  849. case Token_break:
  850. case Token_continue:
  851. case Token_fallthrough:
  852. case Token_if:
  853. case Token_for:
  854. case Token_defer:
  855. case Token_return:
  856. if (token_pos_are_equal(t.pos, f->fix_prev_pos) &&
  857. f->fix_count < PARSER_MAX_FIX_COUNT) {
  858. f->fix_count++;
  859. return;
  860. }
  861. if (token_pos_cmp(f->fix_prev_pos, t.pos) < 0) {
  862. f->fix_prev_pos = t.pos;
  863. f->fix_count = 0; // NOTE(bill): Reset
  864. return;
  865. }
  866. }
  867. next_token(f);
  868. }
  869. #endif
  870. }
  871. b32 expect_semicolon_after_stmt(AstFile *f, AstNode *s) {
  872. // if (s != NULL) {
  873. // switch (s->kind) {
  874. // case AstNode_ProcDecl:
  875. // return true;
  876. // case AstNode_TypeDecl: {
  877. // switch (s->TypeDecl.type->kind) {
  878. // case AstNode_StructType:
  879. // case AstNode_UnionType:
  880. // case AstNode_EnumType:
  881. // case AstNode_ProcType:
  882. // return true;
  883. // }
  884. // } break;
  885. // }
  886. // }
  887. if (!allow_token(f, Token_Semicolon)) {
  888. if (f->cursor[0].pos.line == f->cursor[-1].pos.line) {
  889. if (f->cursor[0].kind != Token_CloseBrace) {
  890. // CLEANUP(bill): Semicolon handling in parser
  891. ast_file_err(f, f->cursor[0],
  892. "Expected `;` after %.*s, got `%.*s`",
  893. LIT(ast_node_strings[s->kind]), LIT(token_strings[f->cursor[0].kind]));
  894. return false;
  895. }
  896. }
  897. }
  898. return true;
  899. }
  900. AstNode *parse_expr(AstFile *f, b32 lhs);
  901. AstNode *parse_proc_type(AstFile *f);
  902. AstNode *parse_stmt_list(AstFile *f, isize *list_count_);
  903. AstNode *parse_stmt(AstFile *f);
  904. AstNode *parse_body(AstFile *f);
  905. AstNode *parse_identifier(AstFile *f) {
  906. Token token = f->cursor[0];
  907. if (token.kind == Token_Identifier) {
  908. next_token(f);
  909. } else {
  910. token.string = make_string("_");
  911. expect_token(f, Token_Identifier);
  912. }
  913. return make_ident(f, token);
  914. }
  915. AstNode *parse_tag_expr(AstFile *f, AstNode *expression) {
  916. Token token = expect_token(f, Token_Hash);
  917. Token name = expect_token(f, Token_Identifier);
  918. return make_tag_expr(f, token, name, expression);
  919. }
  920. AstNode *parse_tag_stmt(AstFile *f, AstNode *statement) {
  921. Token token = expect_token(f, Token_Hash);
  922. Token name = expect_token(f, Token_Identifier);
  923. return make_tag_stmt(f, token, name, statement);
  924. }
  925. AstNode *unparen_expr(AstNode *node) {
  926. for (;;) {
  927. if (node->kind != AstNode_ParenExpr)
  928. return node;
  929. node = node->ParenExpr.expr;
  930. }
  931. }
  932. AstNode *parse_value(AstFile *f);
  933. AstNode *parse_element_list(AstFile *f, isize *element_count_) {
  934. AstNode *root = NULL;
  935. AstNode *curr = NULL;
  936. isize element_count = 0;
  937. while (f->cursor[0].kind != Token_CloseBrace &&
  938. f->cursor[0].kind != Token_EOF) {
  939. AstNode *elem = parse_value(f);
  940. if (f->cursor[0].kind == Token_Eq) {
  941. Token eq = expect_token(f, Token_Eq);
  942. AstNode *value = parse_value(f);
  943. elem = make_field_value(f, elem, value, eq);
  944. }
  945. DLIST_APPEND(root, curr, elem);
  946. element_count++;
  947. if (f->cursor[0].kind != Token_Comma)
  948. break;
  949. next_token(f);
  950. }
  951. if (element_count_) *element_count_ = element_count;
  952. return root;
  953. }
  954. AstNode *parse_literal_value(AstFile *f, AstNode *type) {
  955. AstNode *element_list = NULL;
  956. isize element_count = 0;
  957. Token open = expect_token(f, Token_OpenBrace);
  958. f->expr_level++;
  959. if (f->cursor[0].kind != Token_CloseBrace)
  960. element_list = parse_element_list(f, &element_count);
  961. f->expr_level--;
  962. Token close = expect_token(f, Token_CloseBrace);
  963. return make_compound_lit(f, type, element_list, element_count, open, close);
  964. }
  965. AstNode *parse_value(AstFile *f) {
  966. if (f->cursor[0].kind == Token_OpenBrace)
  967. return parse_literal_value(f, NULL);
  968. AstNode *value = parse_expr(f, false);
  969. return value;
  970. }
  971. AstNode *parse_identifier_or_type(AstFile *f);
  972. void check_proc_add_tag(AstFile *f, AstNode *tag_expr, u64 *tags, ProcTag tag, String tag_name) {
  973. if (*tags & tag) {
  974. ast_file_err(f, ast_node_token(tag_expr), "Procedure tag already used: %.*s", LIT(tag_name));
  975. }
  976. *tags |= tag;
  977. }
  978. b32 is_foreign_name_valid(String name) {
  979. // TODO(bill): is_foreign_name_valid
  980. if (name.len == 0)
  981. return false;
  982. isize offset = 0;
  983. while (offset < name.len) {
  984. Rune rune;
  985. isize remaining = name.len - offset;
  986. isize width = gb_utf8_decode(name.text+offset, remaining, &rune);
  987. if (rune == GB_RUNE_INVALID && width == 1) {
  988. return false;
  989. } else if (rune == GB_RUNE_BOM && remaining > 0) {
  990. return false;
  991. }
  992. if (offset == 0) {
  993. switch (rune) {
  994. case '-':
  995. case '$':
  996. case '.':
  997. case '_':
  998. break;
  999. default:
  1000. if (!gb_char_is_alpha(cast(char)rune))
  1001. return false;
  1002. break;
  1003. }
  1004. } else {
  1005. switch (rune) {
  1006. case '-':
  1007. case '$':
  1008. case '.':
  1009. case '_':
  1010. break;
  1011. default:
  1012. if (!gb_char_is_alphanumeric(cast(char)rune)) {
  1013. return false;
  1014. }
  1015. break;
  1016. }
  1017. }
  1018. offset += width;
  1019. }
  1020. return true;
  1021. }
  1022. void parse_proc_tags(AstFile *f, u64 *tags, String *foreign_name) {
  1023. // TODO(bill): Add this to procedure literals too
  1024. while (f->cursor[0].kind == Token_Hash) {
  1025. AstNode *tag_expr = parse_tag_expr(f, NULL);
  1026. ast_node(te, TagExpr, tag_expr);
  1027. String tag_name = te->name.string;
  1028. if (are_strings_equal(tag_name, make_string("foreign"))) {
  1029. check_proc_add_tag(f, tag_expr, tags, ProcTag_foreign, tag_name);
  1030. if (f->cursor[0].kind == Token_String) {
  1031. *foreign_name = f->cursor[0].string;
  1032. // TODO(bill): Check if valid string
  1033. if (!is_foreign_name_valid(*foreign_name)) {
  1034. ast_file_err(f, ast_node_token(tag_expr), "Invalid alternative foreign procedure name");
  1035. }
  1036. next_token(f);
  1037. }
  1038. } else if (are_strings_equal(tag_name, make_string("inline"))) {
  1039. check_proc_add_tag(f, tag_expr, tags, ProcTag_inline, tag_name);
  1040. } else if (are_strings_equal(tag_name, make_string("no_inline"))) {
  1041. check_proc_add_tag(f, tag_expr, tags, ProcTag_no_inline, tag_name);
  1042. } else {
  1043. ast_file_err(f, ast_node_token(tag_expr), "Unknown procedure tag");
  1044. }
  1045. }
  1046. if ((*tags & ProcTag_inline) && (*tags & ProcTag_no_inline)) {
  1047. ast_file_err(f, f->cursor[0], "You cannot apply both `inline` and `no_inline` to a procedure");
  1048. }
  1049. }
  1050. AstNode *parse_operand(AstFile *f, b32 lhs) {
  1051. AstNode *operand = NULL; // Operand
  1052. switch (f->cursor[0].kind) {
  1053. case Token_Identifier:
  1054. operand = parse_identifier(f);
  1055. if (!lhs) {
  1056. // TODO(bill): Handle?
  1057. }
  1058. return operand;
  1059. case Token_Integer:
  1060. case Token_Float:
  1061. case Token_String:
  1062. case Token_Rune:
  1063. operand = make_basic_lit(f, f->cursor[0]);
  1064. next_token(f);
  1065. return operand;
  1066. case Token_OpenParen: {
  1067. Token open, close;
  1068. // NOTE(bill): Skip the Paren Expression
  1069. open = expect_token(f, Token_OpenParen);
  1070. f->expr_level++;
  1071. operand = parse_expr(f, false);
  1072. f->expr_level--;
  1073. close = expect_token(f, Token_CloseParen);
  1074. return make_paren_expr(f, operand, open, close);
  1075. }
  1076. case Token_Hash: {
  1077. operand = parse_tag_expr(f, NULL);
  1078. String name = operand->TagExpr.name.string;
  1079. if (are_strings_equal(name, make_string("rune"))) {
  1080. if (f->cursor[0].kind == Token_String) {
  1081. Token *s = &f->cursor[0];
  1082. if (gb_utf8_strnlen(s->string.text, s->string.len) != 1) {
  1083. ast_file_err(f, *s, "Invalid rune literal %.*s", LIT(s->string));
  1084. }
  1085. s->kind = Token_Rune; // NOTE(bill): Change it
  1086. } else {
  1087. expect_token(f, Token_String);
  1088. }
  1089. operand = parse_operand(f, lhs);
  1090. } else {
  1091. operand->TagExpr.expr = parse_expr(f, false);
  1092. }
  1093. return operand;
  1094. }
  1095. // Parse Procedure Type or Literal
  1096. case Token_proc: {
  1097. AstNode *curr_proc = f->curr_proc;
  1098. AstNode *type = parse_proc_type(f);
  1099. f->curr_proc = type;
  1100. defer (f->curr_proc = curr_proc);
  1101. u64 tags = 0;
  1102. String foreign_name = {};
  1103. parse_proc_tags(f, &tags, &foreign_name);
  1104. if (tags & ProcTag_foreign) {
  1105. ast_file_err(f, f->cursor[0], "#foreign cannot be applied to procedure literals");
  1106. }
  1107. if (f->cursor[0].kind != Token_OpenBrace) {
  1108. return type;
  1109. } else {
  1110. AstNode *body;
  1111. f->expr_level++;
  1112. body = parse_body(f);
  1113. f->expr_level--;
  1114. return make_proc_lit(f, type, body, tags);
  1115. }
  1116. }
  1117. default: {
  1118. AstNode *type = parse_identifier_or_type(f);
  1119. if (type != NULL) {
  1120. // NOTE(bill): Sanity check as identifiers should be handled already
  1121. GB_ASSERT_MSG(type->kind != AstNode_Ident, "Type Cannot be identifier");
  1122. return type;
  1123. }
  1124. }
  1125. }
  1126. Token begin = f->cursor[0];
  1127. ast_file_err(f, begin, "Expected an operand");
  1128. fix_advance_to_next_stmt(f);
  1129. return make_bad_expr(f, begin, f->cursor[0]);
  1130. }
  1131. b32 is_literal_type(AstNode *node) {
  1132. switch (node->kind) {
  1133. case AstNode_BadExpr:
  1134. case AstNode_Ident:
  1135. case AstNode_SelectorExpr:
  1136. case AstNode_ArrayType:
  1137. case AstNode_VectorType:
  1138. case AstNode_StructType:
  1139. return true;
  1140. }
  1141. return false;
  1142. }
  1143. AstNode *parse_call_expr(AstFile *f, AstNode *operand) {
  1144. AstNode *arg_list = NULL;
  1145. AstNode *arg_list_curr = NULL;
  1146. isize arg_list_count = 0;
  1147. Token open_paren, close_paren;
  1148. f->expr_level++;
  1149. open_paren = expect_token(f, Token_OpenParen);
  1150. while (f->cursor[0].kind != Token_CloseParen &&
  1151. f->cursor[0].kind != Token_EOF) {
  1152. if (f->cursor[0].kind == Token_Comma)
  1153. ast_file_err(f, f->cursor[0], "Expected an expression not a ,");
  1154. DLIST_APPEND(arg_list, arg_list_curr, parse_expr(f, false));
  1155. arg_list_count++;
  1156. if (f->cursor[0].kind != Token_Comma) {
  1157. if (f->cursor[0].kind == Token_CloseParen)
  1158. break;
  1159. }
  1160. next_token(f);
  1161. }
  1162. f->expr_level--;
  1163. close_paren = expect_token(f, Token_CloseParen);
  1164. return make_call_expr(f, operand, arg_list, arg_list_count, open_paren, close_paren);
  1165. }
  1166. AstNode *parse_atom_expr(AstFile *f, b32 lhs) {
  1167. AstNode *operand = parse_operand(f, lhs);
  1168. b32 loop = true;
  1169. while (loop) {
  1170. switch (f->cursor[0].kind) {
  1171. case Token_Prime: {
  1172. Token op = expect_token(f, Token_Prime);
  1173. if (lhs) {
  1174. // TODO(bill): Handle this
  1175. }
  1176. AstNode *proc = parse_identifier(f);
  1177. operand = make_call_expr(f, proc, operand, 1, ast_node_token(operand), op);
  1178. } break;
  1179. case Token_OpenParen: {
  1180. if (lhs) {
  1181. // TODO(bill): Handle this shit! Is this even allowed in this language?!
  1182. }
  1183. operand = parse_call_expr(f, operand);
  1184. } break;
  1185. case Token_Period: {
  1186. Token token = f->cursor[0];
  1187. next_token(f);
  1188. if (lhs) {
  1189. // TODO(bill): handle this
  1190. }
  1191. switch (f->cursor[0].kind) {
  1192. case Token_Identifier:
  1193. operand = make_selector_expr(f, token, operand, parse_identifier(f));
  1194. break;
  1195. default: {
  1196. ast_file_err(f, f->cursor[0], "Expected a selector");
  1197. next_token(f);
  1198. operand = make_selector_expr(f, f->cursor[0], operand, NULL);
  1199. } break;
  1200. }
  1201. } break;
  1202. case Token_OpenBracket: {
  1203. if (lhs) {
  1204. // TODO(bill): Handle this
  1205. }
  1206. Token open, close;
  1207. AstNode *indices[3] = {};
  1208. f->expr_level++;
  1209. open = expect_token(f, Token_OpenBracket);
  1210. if (f->cursor[0].kind != Token_Colon)
  1211. indices[0] = parse_expr(f, false);
  1212. isize colon_count = 0;
  1213. Token colons[2] = {};
  1214. while (f->cursor[0].kind == Token_Colon && colon_count < 2) {
  1215. colons[colon_count++] = f->cursor[0];
  1216. next_token(f);
  1217. if (f->cursor[0].kind != Token_Colon &&
  1218. f->cursor[0].kind != Token_CloseBracket &&
  1219. f->cursor[0].kind != Token_EOF) {
  1220. indices[colon_count] = parse_expr(f, false);
  1221. }
  1222. }
  1223. f->expr_level--;
  1224. close = expect_token(f, Token_CloseBracket);
  1225. if (colon_count == 0) {
  1226. operand = make_index_expr(f, operand, indices[0], open, close);
  1227. } else {
  1228. b32 triple_indexed = false;
  1229. if (colon_count == 2) {
  1230. triple_indexed = true;
  1231. if (indices[1] == NULL) {
  1232. ast_file_err(f, colons[0], "Second index is required in a triple indexed slice");
  1233. indices[1] = make_bad_expr(f, colons[0], colons[1]);
  1234. }
  1235. if (indices[2] == NULL) {
  1236. ast_file_err(f, colons[1], "Third index is required in a triple indexed slice");
  1237. indices[2] = make_bad_expr(f, colons[1], close);
  1238. }
  1239. }
  1240. operand = make_slice_expr(f, operand, open, close, indices[0], indices[1], indices[2], triple_indexed);
  1241. }
  1242. } break;
  1243. case Token_Pointer: // Deference
  1244. operand = make_deref_expr(f, operand, expect_token(f, Token_Pointer));
  1245. break;
  1246. case Token_OpenBrace: {
  1247. if (!lhs && is_literal_type(operand) && f->expr_level >= 0) {
  1248. operand = parse_literal_value(f, operand);
  1249. } else {
  1250. loop = false;
  1251. }
  1252. } break;
  1253. default:
  1254. loop = false;
  1255. break;
  1256. }
  1257. lhs = false; // NOTE(bill): 'tis not lhs anymore
  1258. }
  1259. return operand;
  1260. }
  1261. AstNode *parse_type(AstFile *f);
  1262. AstNode *parse_unary_expr(AstFile *f, b32 lhs) {
  1263. switch (f->cursor[0].kind) {
  1264. case Token_Pointer:
  1265. case Token_Add:
  1266. case Token_Sub:
  1267. case Token_Not:
  1268. case Token_Xor: {
  1269. AstNode *operand;
  1270. Token op = f->cursor[0];
  1271. next_token(f);
  1272. operand = parse_unary_expr(f, lhs);
  1273. return make_unary_expr(f, op, operand);
  1274. } break;
  1275. }
  1276. return parse_atom_expr(f, lhs);
  1277. }
  1278. AstNode *parse_binary_expr(AstFile *f, b32 lhs, i32 prec_in) {
  1279. AstNode *expression = parse_unary_expr(f, lhs);
  1280. for (i32 prec = token_precedence(f->cursor[0]); prec >= prec_in; prec--) {
  1281. for (;;) {
  1282. AstNode *right;
  1283. Token op = f->cursor[0];
  1284. i32 op_prec = token_precedence(op);
  1285. if (op_prec != prec)
  1286. break;
  1287. expect_operator(f); // NOTE(bill): error checks too
  1288. if (lhs) {
  1289. // TODO(bill): error checking
  1290. lhs = false;
  1291. }
  1292. switch (op.kind) {
  1293. case Token_DoublePrime: {
  1294. AstNode *proc = parse_identifier(f);
  1295. AstNode *right = parse_binary_expr(f, false, prec+1);
  1296. expression->next = right;
  1297. expression = make_call_expr(f, proc, expression, 2, op, ast_node_token(right));
  1298. continue;
  1299. } break;
  1300. case Token_as:
  1301. case Token_transmute:
  1302. case Token_down_cast:
  1303. right = parse_type(f);
  1304. break;
  1305. default:
  1306. right = parse_binary_expr(f, false, prec+1);
  1307. if (!right) {
  1308. ast_file_err(f, op, "Expected expression on the right hand side of the binary operator");
  1309. }
  1310. break;
  1311. }
  1312. expression = make_binary_expr(f, op, expression, right);
  1313. }
  1314. }
  1315. return expression;
  1316. }
  1317. AstNode *parse_expr(AstFile *f, b32 lhs) {
  1318. return parse_binary_expr(f, lhs, 0+1);
  1319. }
  1320. AstNode *parse_expr_list(AstFile *f, b32 lhs, isize *list_count_) {
  1321. AstNode *list_root = NULL;
  1322. AstNode *list_curr = NULL;
  1323. isize list_count = 0;
  1324. do {
  1325. AstNode *e = parse_expr(f, lhs);
  1326. DLIST_APPEND(list_root, list_curr, e);
  1327. list_count++;
  1328. if (f->cursor[0].kind != Token_Comma ||
  1329. f->cursor[0].kind == Token_EOF)
  1330. break;
  1331. next_token(f);
  1332. } while (true);
  1333. if (list_count_) *list_count_ = list_count;
  1334. return list_root;
  1335. }
  1336. AstNode *parse_lhs_expr_list(AstFile *f, isize *list_count) {
  1337. return parse_expr_list(f, true, list_count);
  1338. }
  1339. AstNode *parse_rhs_expr_list(AstFile *f, isize *list_count) {
  1340. return parse_expr_list(f, false, list_count);
  1341. }
  1342. AstNode *parse_decl(AstFile *f, AstNode *name_list, isize name_count);
  1343. AstNode *parse_simple_stmt(AstFile *f) {
  1344. isize lhs_count = 0, rhs_count = 0;
  1345. AstNode *lhs_expr_list = parse_lhs_expr_list(f, &lhs_count);
  1346. AstNode *statement = NULL;
  1347. Token token = f->cursor[0];
  1348. switch (token.kind) {
  1349. case Token_Eq:
  1350. case Token_AddEq:
  1351. case Token_SubEq:
  1352. case Token_MulEq:
  1353. case Token_QuoEq:
  1354. case Token_ModEq:
  1355. case Token_AndEq:
  1356. case Token_OrEq:
  1357. case Token_XorEq:
  1358. case Token_ShlEq:
  1359. case Token_ShrEq:
  1360. case Token_AndNotEq:
  1361. case Token_CmpAndEq:
  1362. case Token_CmpOrEq:
  1363. {
  1364. if (f->curr_proc == NULL) {
  1365. ast_file_err(f, f->cursor[0], "You cannot use a simple statement in the file scope");
  1366. return make_bad_stmt(f, f->cursor[0], f->cursor[0]);
  1367. }
  1368. next_token(f);
  1369. AstNode *rhs_expr_list = parse_rhs_expr_list(f, &rhs_count);
  1370. if (rhs_expr_list == NULL) {
  1371. ast_file_err(f, token, "No right-hand side in assignment statement.");
  1372. return make_bad_stmt(f, token, f->cursor[0]);
  1373. }
  1374. return make_assign_stmt(f, token,
  1375. lhs_expr_list, lhs_count,
  1376. rhs_expr_list, rhs_count);
  1377. } break;
  1378. case Token_Colon: // Declare
  1379. return parse_decl(f, lhs_expr_list, lhs_count);
  1380. }
  1381. if (lhs_count > 1) {
  1382. ast_file_err(f, token, "Expected 1 expression");
  1383. return make_bad_stmt(f, token, f->cursor[0]);
  1384. }
  1385. token = f->cursor[0];
  1386. switch (token.kind) {
  1387. case Token_Increment:
  1388. case Token_Decrement:
  1389. if (f->curr_proc == NULL) {
  1390. ast_file_err(f, f->cursor[0], "You cannot use a simple statement in the file scope");
  1391. return make_bad_stmt(f, f->cursor[0], f->cursor[0]);
  1392. }
  1393. statement = make_inc_dec_stmt(f, token, lhs_expr_list);
  1394. next_token(f);
  1395. return statement;
  1396. }
  1397. return make_expr_stmt(f, lhs_expr_list);
  1398. }
  1399. AstNode *parse_block_stmt(AstFile *f) {
  1400. if (f->curr_proc == NULL) {
  1401. ast_file_err(f, f->cursor[0], "You cannot use a block statement in the file scope");
  1402. return make_bad_stmt(f, f->cursor[0], f->cursor[0]);
  1403. }
  1404. AstNode *block_stmt = parse_body(f);
  1405. return block_stmt;
  1406. }
  1407. AstNode *convert_stmt_to_expr(AstFile *f, AstNode *statement, String kind) {
  1408. if (statement == NULL)
  1409. return NULL;
  1410. if (statement->kind == AstNode_ExprStmt)
  1411. return statement->ExprStmt.expr;
  1412. ast_file_err(f, f->cursor[0], "Expected `%.*s`, found a simple statement.", LIT(kind));
  1413. return make_bad_expr(f, f->cursor[0], f->cursor[1]);
  1414. }
  1415. AstNode *parse_identfier_list(AstFile *f, isize *list_count_) {
  1416. AstNode *list_root = NULL;
  1417. AstNode *list_curr = NULL;
  1418. isize list_count = 0;
  1419. do {
  1420. DLIST_APPEND(list_root, list_curr, parse_identifier(f));
  1421. list_count++;
  1422. if (f->cursor[0].kind != Token_Comma ||
  1423. f->cursor[0].kind == Token_EOF)
  1424. break;
  1425. next_token(f);
  1426. } while (true);
  1427. if (list_count_) *list_count_ = list_count;
  1428. return list_root;
  1429. }
  1430. AstNode *parse_type_attempt(AstFile *f) {
  1431. AstNode *type = parse_identifier_or_type(f);
  1432. if (type != NULL) {
  1433. // TODO(bill): Handle?
  1434. }
  1435. return type;
  1436. }
  1437. AstNode *parse_type(AstFile *f) {
  1438. AstNode *type = parse_type_attempt(f);
  1439. if (type == NULL) {
  1440. Token token = f->cursor[0];
  1441. ast_file_err(f, token, "Expected a type");
  1442. next_token(f);
  1443. return make_bad_expr(f, token, f->cursor[0]);
  1444. }
  1445. return type;
  1446. }
  1447. Token parse_procedure_signature(AstFile *f,
  1448. AstNode **param_list, isize *param_count,
  1449. AstNode **result_list, isize *result_count);
  1450. AstNode *parse_proc_type(AstFile *f) {
  1451. AstNode *params = NULL;
  1452. AstNode *results = NULL;
  1453. isize param_count = 0;
  1454. isize result_count = 0;
  1455. Token proc_token = parse_procedure_signature(f, &params, &param_count, &results, &result_count);
  1456. return make_proc_type(f, proc_token, params, param_count, results, result_count);
  1457. }
  1458. AstNode *parse_field_decl(AstFile *f) {
  1459. AstNode *name_list = NULL;
  1460. isize name_count = 0;
  1461. b32 is_using = false;
  1462. if (allow_token(f, Token_using)) {
  1463. is_using = true;
  1464. }
  1465. name_list = parse_lhs_expr_list(f, &name_count);
  1466. if (name_count == 0) {
  1467. ast_file_err(f, f->cursor[0], "Empty field declaration");
  1468. }
  1469. if (name_count > 1 && is_using) {
  1470. ast_file_err(f, f->cursor[0], "Cannot apply `using` to more than one of the same type");
  1471. is_using = false;
  1472. }
  1473. expect_token(f, Token_Colon);
  1474. AstNode *type = NULL;
  1475. if (f->cursor[0].kind == Token_Ellipsis) {
  1476. Token ellipsis = f->cursor[0];
  1477. next_token(f);
  1478. type = parse_type_attempt(f);
  1479. if (type == NULL) {
  1480. ast_file_err(f, f->cursor[0], "variadic parameter is missing a type after `..`");
  1481. type = make_bad_expr(f, ellipsis, f->cursor[0]);
  1482. } else {
  1483. if (name_count > 1) {
  1484. ast_file_err(f, f->cursor[0], "mutliple variadic parameters, only `..`");
  1485. } else {
  1486. type = make_ellipsis(f, ellipsis, type);
  1487. }
  1488. }
  1489. } else {
  1490. type = parse_type_attempt(f);
  1491. }
  1492. if (type == NULL) {
  1493. ast_file_err(f, f->cursor[0], "Expected a type for this field declaration");
  1494. }
  1495. AstNode *field = make_field(f, name_list, name_count, type, is_using);
  1496. return field;
  1497. }
  1498. AstNode *parse_parameter_list(AstFile *f, isize *param_count_) {
  1499. AstNode *param_list = NULL;
  1500. AstNode *param_list_curr = NULL;
  1501. isize param_count = 0;
  1502. while (f->cursor[0].kind == Token_Identifier ||
  1503. f->cursor[0].kind == Token_using) {
  1504. AstNode *field = parse_field_decl(f);
  1505. DLIST_APPEND(param_list, param_list_curr, field);
  1506. param_count += field->Field.name_count;
  1507. if (f->cursor[0].kind != Token_Comma)
  1508. break;
  1509. next_token(f);
  1510. }
  1511. if (param_count_) *param_count_ = param_count;
  1512. return param_list;
  1513. }
  1514. AstNode *parse_struct_params(AstFile *f, isize *decl_count_, b32 using_allowed) {
  1515. AstNode *decls = NULL;
  1516. AstNode *decls_curr = NULL;
  1517. isize decl_count = 0;
  1518. while (f->cursor[0].kind == Token_Identifier ||
  1519. f->cursor[0].kind == Token_using) {
  1520. b32 is_using = false;
  1521. if (allow_token(f, Token_using)) {
  1522. is_using = true;
  1523. }
  1524. isize name_count = 0;
  1525. AstNode *name_list = parse_lhs_expr_list(f, &name_count);
  1526. if (name_count == 0) {
  1527. ast_file_err(f, f->cursor[0], "Empty field declaration");
  1528. }
  1529. if (!using_allowed && is_using) {
  1530. ast_file_err(f, f->cursor[0], "Cannot apply `using` to members of a union");
  1531. is_using = false;
  1532. }
  1533. if (name_count > 1 && is_using) {
  1534. ast_file_err(f, f->cursor[0], "Cannot apply `using` to more than one of the same type");
  1535. }
  1536. AstNode *decl = NULL;
  1537. if (f->cursor[0].kind == Token_Colon) {
  1538. decl = parse_decl(f, name_list, name_count);
  1539. if (decl->kind == AstNode_ProcDecl) {
  1540. ast_file_err(f, f->cursor[0], "Procedure declarations are not allowed within a structure");
  1541. decl = make_bad_decl(f, ast_node_token(name_list), f->cursor[0]);
  1542. }
  1543. } else {
  1544. ast_file_err(f, f->cursor[0], "Illegal structure field");
  1545. decl = make_bad_decl(f, ast_node_token(name_list), f->cursor[0]);
  1546. }
  1547. expect_semicolon_after_stmt(f, decl);
  1548. if (decl != NULL && is_ast_node_decl(decl)) {
  1549. DLIST_APPEND(decls, decls_curr, decl);
  1550. if (decl->kind == AstNode_VarDecl) {
  1551. decl_count += decl->VarDecl.name_count;
  1552. decl->VarDecl.is_using = is_using && using_allowed;
  1553. if (decl->VarDecl.kind == Declaration_Mutable) {
  1554. if (decl->VarDecl.value_count > 0) {
  1555. ast_file_err(f, f->cursor[0], "Default variable assignments within a structure will be ignored (at the moment)");
  1556. }
  1557. }
  1558. } else {
  1559. decl_count += 1;
  1560. }
  1561. }
  1562. }
  1563. if (decl_count_) *decl_count_ = decl_count;
  1564. return decls;
  1565. }
  1566. AstNode *parse_identifier_or_type(AstFile *f) {
  1567. switch (f->cursor[0].kind) {
  1568. case Token_Identifier: {
  1569. AstNode *e = parse_identifier(f);
  1570. while (f->cursor[0].kind == Token_Period) {
  1571. Token token = f->cursor[0];
  1572. next_token(f);
  1573. AstNode *sel = parse_identifier(f);
  1574. e = make_selector_expr(f, token, e, sel);
  1575. }
  1576. if (f->cursor[0].kind == Token_OpenParen) {
  1577. // HACK NOTE(bill): For type_of_val(expr)
  1578. e = parse_call_expr(f, e);
  1579. }
  1580. return e;
  1581. }
  1582. case Token_Pointer:
  1583. return make_pointer_type(f, expect_token(f, Token_Pointer), parse_type(f));
  1584. case Token_OpenBracket: {
  1585. f->expr_level++;
  1586. Token token = expect_token(f, Token_OpenBracket);
  1587. AstNode *count_expr = NULL;
  1588. if (f->cursor[0].kind == Token_Ellipsis) {
  1589. count_expr = make_ellipsis(f, f->cursor[0], NULL);
  1590. next_token(f);
  1591. } else if (f->cursor[0].kind != Token_CloseBracket) {
  1592. count_expr = parse_expr(f, false);
  1593. }
  1594. expect_token(f, Token_CloseBracket);
  1595. f->expr_level--;
  1596. return make_array_type(f, token, count_expr, parse_type(f));
  1597. }
  1598. case Token_OpenBrace: {
  1599. f->expr_level++;
  1600. Token token = expect_token(f, Token_OpenBrace);
  1601. AstNode *count_expr = parse_expr(f, false);
  1602. expect_token(f, Token_CloseBrace);
  1603. f->expr_level--;
  1604. return make_vector_type(f, token, count_expr, parse_type(f));
  1605. }
  1606. case Token_struct: {
  1607. Token token = expect_token(f, Token_struct);
  1608. b32 is_packed = false;
  1609. if (allow_token(f, Token_Hash)) {
  1610. Token tag = expect_token(f, Token_Identifier);
  1611. if (are_strings_equal(tag.string, make_string("packed"))) {
  1612. is_packed = true;
  1613. } else {
  1614. ast_file_err(f, tag, "Expected a `#packed` tag");
  1615. }
  1616. }
  1617. Token open = expect_token(f, Token_OpenBrace);
  1618. isize decl_count = 0;
  1619. AstNode *decls = parse_struct_params(f, &decl_count, true);
  1620. Token close = expect_token(f, Token_CloseBrace);
  1621. return make_struct_type(f, token, decls, decl_count, is_packed);
  1622. } break;
  1623. case Token_union: {
  1624. Token token = expect_token(f, Token_union);
  1625. Token open = expect_token(f, Token_OpenBrace);
  1626. isize decl_count = 0;
  1627. AstNode *decls = parse_struct_params(f, &decl_count, false);
  1628. Token close = expect_token(f, Token_CloseBrace);
  1629. return make_union_type(f, token, decls, decl_count);
  1630. }
  1631. case Token_raw_union: {
  1632. Token token = expect_token(f, Token_raw_union);
  1633. Token open = expect_token(f, Token_OpenBrace);
  1634. isize decl_count = 0;
  1635. AstNode *decls = parse_struct_params(f, &decl_count, true);
  1636. Token close = expect_token(f, Token_CloseBrace);
  1637. return make_raw_union_type(f, token, decls, decl_count);
  1638. }
  1639. case Token_enum: {
  1640. Token token = expect_token(f, Token_enum);
  1641. AstNode *base_type = NULL;
  1642. Token open, close;
  1643. if (f->cursor[0].kind != Token_OpenBrace) {
  1644. base_type = parse_type(f);
  1645. }
  1646. AstNode *root = NULL;
  1647. AstNode *curr = NULL;
  1648. isize field_count = 0;
  1649. open = expect_token(f, Token_OpenBrace);
  1650. while (f->cursor[0].kind != Token_CloseBrace &&
  1651. f->cursor[0].kind != Token_EOF) {
  1652. AstNode *name = parse_identifier(f);
  1653. AstNode *value = NULL;
  1654. Token eq = empty_token;
  1655. if (f->cursor[0].kind == Token_Eq) {
  1656. eq = expect_token(f, Token_Eq);
  1657. value = parse_value(f);
  1658. }
  1659. AstNode *field = make_field_value(f, name, value, eq);
  1660. DLIST_APPEND(root, curr, field);
  1661. field_count++;
  1662. if (f->cursor[0].kind != Token_Comma)
  1663. break;
  1664. next_token(f);
  1665. }
  1666. close = expect_token(f, Token_CloseBrace);
  1667. return make_enum_type(f, token, base_type, root, field_count);
  1668. }
  1669. case Token_proc: {
  1670. AstNode *curr_proc = f->curr_proc;
  1671. AstNode *type = parse_proc_type(f);
  1672. f->curr_proc = type;
  1673. f->curr_proc = curr_proc;
  1674. return type;
  1675. }
  1676. case Token_OpenParen: {
  1677. // NOTE(bill): Skip the paren expression
  1678. AstNode *type;
  1679. Token open, close;
  1680. open = expect_token(f, Token_OpenParen);
  1681. type = parse_type(f);
  1682. close = expect_token(f, Token_CloseParen);
  1683. return make_paren_expr(f, type, open, close);
  1684. }
  1685. // TODO(bill): Why is this even allowed? Is this a parsing error?
  1686. case Token_Colon:
  1687. break;
  1688. case Token_Eq:
  1689. if (f->cursor[-1].kind == Token_Colon)
  1690. break;
  1691. // fallthrough
  1692. default:
  1693. ast_file_err(f, f->cursor[0],
  1694. "Expected a type after `%.*s`, got `%.*s`", LIT(f->cursor[-1].string), LIT(f->cursor[0].string));
  1695. break;
  1696. }
  1697. return NULL;
  1698. }
  1699. AstNode *parse_results(AstFile *f, isize *result_count) {
  1700. if (allow_token(f, Token_ArrowRight)) {
  1701. if (f->cursor[0].kind == Token_OpenParen) {
  1702. expect_token(f, Token_OpenParen);
  1703. AstNode *list = NULL;
  1704. AstNode *list_curr = NULL;
  1705. isize count = 0;
  1706. while (f->cursor[0].kind != Token_CloseParen &&
  1707. f->cursor[0].kind != Token_EOF) {
  1708. DLIST_APPEND(list, list_curr, parse_type(f));
  1709. count++;
  1710. if (f->cursor[0].kind != Token_Comma)
  1711. break;
  1712. next_token(f);
  1713. }
  1714. expect_token(f, Token_CloseParen);
  1715. if (result_count) *result_count = count;
  1716. return list;
  1717. }
  1718. AstNode *result = parse_type(f);
  1719. if (result_count) *result_count = 1;
  1720. return result;
  1721. }
  1722. if (result_count) *result_count = 0;
  1723. return NULL;
  1724. }
  1725. Token parse_procedure_signature(AstFile *f,
  1726. AstNode **param_list, isize *param_count,
  1727. AstNode **result_list, isize *result_count) {
  1728. Token proc_token = expect_token(f, Token_proc);
  1729. expect_token(f, Token_OpenParen);
  1730. *param_list = parse_parameter_list(f, param_count);
  1731. expect_token(f, Token_CloseParen);
  1732. *result_list = parse_results(f, result_count);
  1733. return proc_token;
  1734. }
  1735. AstNode *parse_body(AstFile *f) {
  1736. AstNode *statement_list = NULL;
  1737. isize statement_list_count = 0;
  1738. Token open, close;
  1739. open = expect_token(f, Token_OpenBrace);
  1740. statement_list = parse_stmt_list(f, &statement_list_count);
  1741. close = expect_token(f, Token_CloseBrace);
  1742. return make_block_stmt(f, statement_list, statement_list_count, open, close);
  1743. }
  1744. AstNode *parse_proc_decl(AstFile *f, Token proc_token, AstNode *name) {
  1745. AstNode *param_list = NULL;
  1746. AstNode *result_list = NULL;
  1747. isize param_count = 0;
  1748. isize result_count = 0;
  1749. parse_procedure_signature(f, &param_list, &param_count, &result_list, &result_count);
  1750. AstNode *proc_type = make_proc_type(f, proc_token, param_list, param_count, result_list, result_count);
  1751. AstNode *body = NULL;
  1752. u64 tags = 0;
  1753. String foreign_name = {};
  1754. parse_proc_tags(f, &tags, &foreign_name);
  1755. AstNode *curr_proc = f->curr_proc;
  1756. f->curr_proc = proc_type;
  1757. defer (f->curr_proc = curr_proc);
  1758. if (f->cursor[0].kind == Token_OpenBrace) {
  1759. if ((tags & ProcTag_foreign) != 0) {
  1760. ast_file_err(f, f->cursor[0], "A procedure tagged as `#foreign` cannot have a body");
  1761. }
  1762. body = parse_body(f);
  1763. }
  1764. return make_proc_decl(f, name, proc_type, body, tags, foreign_name);
  1765. }
  1766. AstNode *parse_decl(AstFile *f, AstNode *name_list, isize name_count) {
  1767. AstNode *value_list = NULL;
  1768. AstNode *type = NULL;
  1769. isize value_count = 0;
  1770. if (allow_token(f, Token_Colon)) {
  1771. if (!allow_token(f, Token_type)) {
  1772. type = parse_identifier_or_type(f);
  1773. }
  1774. } else if (f->cursor[0].kind != Token_Eq && f->cursor[0].kind != Token_Semicolon) {
  1775. ast_file_err(f, f->cursor[0], "Expected type separator `:` or `=`");
  1776. }
  1777. DeclKind declaration_kind = Declaration_Mutable;
  1778. if (f->cursor[0].kind == Token_Eq ||
  1779. f->cursor[0].kind == Token_Colon) {
  1780. if (f->cursor[0].kind == Token_Colon)
  1781. declaration_kind = Declaration_Immutable;
  1782. next_token(f);
  1783. if (f->cursor[0].kind == Token_type ||
  1784. f->cursor[0].kind == Token_struct ||
  1785. f->cursor[0].kind == Token_enum ||
  1786. f->cursor[0].kind == Token_union ||
  1787. f->cursor[0].kind == Token_raw_union) {
  1788. Token token = f->cursor[0];
  1789. if (token.kind == Token_type) {
  1790. next_token(f);
  1791. }
  1792. if (name_count != 1) {
  1793. ast_file_err(f, ast_node_token(name_list), "You can only declare one type at a time");
  1794. return make_bad_decl(f, name_list->Ident, token);
  1795. }
  1796. if (type != NULL) {
  1797. ast_file_err(f, f->cursor[-1], "Expected either `type` or nothing between : and :");
  1798. // NOTE(bill): Do not fail though
  1799. }
  1800. AstNode *type = parse_type(f);
  1801. return make_type_decl(f, token, name_list, type);
  1802. } else if (f->cursor[0].kind == Token_proc &&
  1803. declaration_kind == Declaration_Immutable) {
  1804. // NOTE(bill): Procedure declarations
  1805. Token proc_token = f->cursor[0];
  1806. AstNode *name = name_list;
  1807. if (name_count != 1) {
  1808. ast_file_err(f, proc_token, "You can only declare one procedure at a time");
  1809. return make_bad_decl(f, name->Ident, proc_token);
  1810. }
  1811. AstNode *proc_decl = parse_proc_decl(f, proc_token, name);
  1812. return proc_decl;
  1813. } else {
  1814. value_list = parse_rhs_expr_list(f, &value_count);
  1815. if (value_count > name_count) {
  1816. ast_file_err(f, f->cursor[0], "Too many values on the right hand side of the declaration");
  1817. } else if (value_count < name_count &&
  1818. declaration_kind == Declaration_Immutable) {
  1819. ast_file_err(f, f->cursor[0], "All constant declarations must be defined");
  1820. } else if (value_list == NULL) {
  1821. ast_file_err(f, f->cursor[0], "Expected an expression for this declaration");
  1822. }
  1823. }
  1824. }
  1825. if (declaration_kind == Declaration_Mutable) {
  1826. if (type == NULL && value_list == NULL) {
  1827. ast_file_err(f, f->cursor[0], "Missing variable type or initialization");
  1828. return make_bad_decl(f, f->cursor[0], f->cursor[0]);
  1829. }
  1830. } else if (declaration_kind == Declaration_Immutable) {
  1831. if (type == NULL && value_list == NULL && name_count > 0) {
  1832. ast_file_err(f, f->cursor[0], "Missing constant value");
  1833. return make_bad_decl(f, f->cursor[0], f->cursor[0]);
  1834. }
  1835. } else {
  1836. Token begin = f->cursor[0];
  1837. ast_file_err(f, begin, "Unknown type of variable declaration");
  1838. fix_advance_to_next_stmt(f);
  1839. return make_bad_decl(f, begin, f->cursor[0]);
  1840. }
  1841. AstNode *var_decl = make_var_decl(f, declaration_kind, name_list, name_count, type, value_list, value_count);
  1842. return var_decl;
  1843. }
  1844. AstNode *parse_if_stmt(AstFile *f) {
  1845. if (f->curr_proc == NULL) {
  1846. ast_file_err(f, f->cursor[0], "You cannot use an if statement in the file scope");
  1847. return make_bad_stmt(f, f->cursor[0], f->cursor[0]);
  1848. }
  1849. Token token = expect_token(f, Token_if);
  1850. AstNode *init = NULL;
  1851. AstNode *cond = NULL;
  1852. AstNode *body = NULL;
  1853. AstNode *else_stmt = NULL;
  1854. isize prev_level = f->expr_level;
  1855. f->expr_level = -1;
  1856. if (allow_token(f, Token_Semicolon)) {
  1857. cond = parse_expr(f, false);
  1858. } else {
  1859. init = parse_simple_stmt(f);
  1860. if (allow_token(f, Token_Semicolon)) {
  1861. cond = parse_expr(f, false);
  1862. } else {
  1863. cond = convert_stmt_to_expr(f, init, make_string("boolean expression"));
  1864. init = NULL;
  1865. }
  1866. }
  1867. f->expr_level = prev_level;
  1868. if (cond == NULL) {
  1869. ast_file_err(f, f->cursor[0], "Expected condition for if statement");
  1870. }
  1871. body = parse_block_stmt(f);
  1872. if (allow_token(f, Token_else)) {
  1873. switch (f->cursor[0].kind) {
  1874. case Token_if:
  1875. else_stmt = parse_if_stmt(f);
  1876. break;
  1877. case Token_OpenBrace:
  1878. else_stmt = parse_block_stmt(f);
  1879. break;
  1880. default:
  1881. ast_file_err(f, f->cursor[0], "Expected if statement block statement");
  1882. else_stmt = make_bad_stmt(f, f->cursor[0], f->cursor[1]);
  1883. break;
  1884. }
  1885. }
  1886. return make_if_stmt(f, token, init, cond, body, else_stmt);
  1887. }
  1888. AstNode *parse_return_stmt(AstFile *f) {
  1889. if (f->curr_proc == NULL) {
  1890. ast_file_err(f, f->cursor[0], "You cannot use a return statement in the file scope");
  1891. return make_bad_stmt(f, f->cursor[0], f->cursor[0]);
  1892. }
  1893. Token token = expect_token(f, Token_return);
  1894. AstNode *result = NULL;
  1895. isize result_count = 0;
  1896. if (f->cursor[0].kind != Token_Semicolon && f->cursor[0].kind != Token_CloseBrace &&
  1897. f->cursor[0].pos.line == token.pos.line) {
  1898. result = parse_rhs_expr_list(f, &result_count);
  1899. }
  1900. if (f->cursor[0].kind != Token_CloseBrace) {
  1901. expect_semicolon_after_stmt(f, result);
  1902. }
  1903. return make_return_stmt(f, token, result, result_count);
  1904. }
  1905. AstNode *parse_for_stmt(AstFile *f) {
  1906. if (f->curr_proc == NULL) {
  1907. ast_file_err(f, f->cursor[0], "You cannot use a for statement in the file scope");
  1908. return make_bad_stmt(f, f->cursor[0], f->cursor[0]);
  1909. }
  1910. Token token = expect_token(f, Token_for);
  1911. AstNode *init = NULL;
  1912. AstNode *cond = NULL;
  1913. AstNode *end = NULL;
  1914. AstNode *body = NULL;
  1915. if (f->cursor[0].kind != Token_OpenBrace) {
  1916. isize prev_level = f->expr_level;
  1917. f->expr_level = -1;
  1918. if (f->cursor[0].kind != Token_Semicolon) {
  1919. cond = parse_simple_stmt(f);
  1920. if (is_ast_node_complex_stmt(cond)) {
  1921. ast_file_err(f, f->cursor[0],
  1922. "You are not allowed that type of statement in a for statement, it is too complex!");
  1923. }
  1924. }
  1925. if (allow_token(f, Token_Semicolon)) {
  1926. init = cond;
  1927. cond = NULL;
  1928. if (f->cursor[0].kind != Token_Semicolon) {
  1929. cond = parse_simple_stmt(f);
  1930. }
  1931. expect_token(f, Token_Semicolon);
  1932. if (f->cursor[0].kind != Token_OpenBrace) {
  1933. end = parse_simple_stmt(f);
  1934. }
  1935. }
  1936. f->expr_level = prev_level;
  1937. }
  1938. body = parse_block_stmt(f);
  1939. cond = convert_stmt_to_expr(f, cond, make_string("boolean expression"));
  1940. return make_for_stmt(f, token, init, cond, end, body);
  1941. }
  1942. AstNode *parse_case_clause(AstFile *f) {
  1943. Token token = f->cursor[0];
  1944. AstNode *list = NULL;
  1945. isize list_count = 0;
  1946. if (allow_token(f, Token_case)) {
  1947. list = parse_rhs_expr_list(f, &list_count);
  1948. } else {
  1949. expect_token(f, Token_default);
  1950. }
  1951. expect_token(f, Token_Colon); // TODO(bill): Is this the best syntax?
  1952. isize stmt_count = 0;
  1953. AstNode *stmts = parse_stmt_list(f, &stmt_count);
  1954. return make_case_clause(f, token, list, list_count, stmts, stmt_count);
  1955. }
  1956. AstNode *parse_type_case_clause(AstFile *f) {
  1957. Token token = f->cursor[0];
  1958. AstNode *clause = NULL;
  1959. if (allow_token(f, Token_case)) {
  1960. clause = parse_expr(f, false);
  1961. } else {
  1962. expect_token(f, Token_default);
  1963. }
  1964. expect_token(f, Token_Colon); // TODO(bill): Is this the best syntax?
  1965. isize stmt_count = 0;
  1966. AstNode *stmts = parse_stmt_list(f, &stmt_count);
  1967. return make_case_clause(f, token, clause, 1, stmts, stmt_count);
  1968. }
  1969. AstNode *parse_match_stmt(AstFile *f) {
  1970. if (f->curr_proc == NULL) {
  1971. ast_file_err(f, f->cursor[0], "You cannot use a match statement in the file scope");
  1972. return make_bad_stmt(f, f->cursor[0], f->cursor[0]);
  1973. }
  1974. Token token = expect_token(f, Token_match);
  1975. AstNode *init = NULL;
  1976. AstNode *tag = NULL;
  1977. AstNode *body = NULL;
  1978. Token open, close;
  1979. if (allow_token(f, Token_type)) {
  1980. AstNode *var = parse_identifier(f);
  1981. expect_token(f, Token_Colon);
  1982. tag = parse_simple_stmt(f);
  1983. open = expect_token(f, Token_OpenBrace);
  1984. AstNode *list = NULL;
  1985. AstNode *list_curr = NULL;
  1986. isize list_count = 0;
  1987. while (f->cursor[0].kind == Token_case ||
  1988. f->cursor[0].kind == Token_default) {
  1989. DLIST_APPEND(list, list_curr, parse_type_case_clause(f));
  1990. list_count++;
  1991. }
  1992. close = expect_token(f, Token_CloseBrace);
  1993. body = make_block_stmt(f, list, list_count, open, close);
  1994. tag = convert_stmt_to_expr(f, tag, make_string("type match expression"));
  1995. return make_type_match_stmt(f, token, tag, var, body);
  1996. } else {
  1997. if (f->cursor[0].kind != Token_OpenBrace) {
  1998. isize prev_level = f->expr_level;
  1999. f->expr_level = -1;
  2000. if (f->cursor[0].kind != Token_Semicolon) {
  2001. tag = parse_simple_stmt(f);
  2002. }
  2003. if (allow_token(f, Token_Semicolon)) {
  2004. init = tag;
  2005. tag = NULL;
  2006. if (f->cursor[0].kind != Token_OpenBrace) {
  2007. tag = parse_simple_stmt(f);
  2008. }
  2009. }
  2010. f->expr_level = prev_level;
  2011. }
  2012. open = expect_token(f, Token_OpenBrace);
  2013. AstNode *list = NULL;
  2014. AstNode *list_curr = NULL;
  2015. isize list_count = 0;
  2016. while (f->cursor[0].kind == Token_case ||
  2017. f->cursor[0].kind == Token_default) {
  2018. DLIST_APPEND(list, list_curr, parse_case_clause(f));
  2019. list_count++;
  2020. }
  2021. close = expect_token(f, Token_CloseBrace);
  2022. body = make_block_stmt(f, list, list_count, open, close);
  2023. tag = convert_stmt_to_expr(f, tag, make_string("match expression"));
  2024. return make_match_stmt(f, token, init, tag, body);
  2025. }
  2026. }
  2027. AstNode *parse_defer_stmt(AstFile *f) {
  2028. if (f->curr_proc == NULL) {
  2029. ast_file_err(f, f->cursor[0], "You cannot use a defer statement in the file scope");
  2030. return make_bad_stmt(f, f->cursor[0], f->cursor[0]);
  2031. }
  2032. Token token = expect_token(f, Token_defer);
  2033. AstNode *statement = parse_stmt(f);
  2034. switch (statement->kind) {
  2035. case AstNode_EmptyStmt:
  2036. ast_file_err(f, token, "Empty statement after defer (e.g. `;`)");
  2037. break;
  2038. case AstNode_DeferStmt:
  2039. ast_file_err(f, token, "You cannot defer a defer statement");
  2040. break;
  2041. case AstNode_ReturnStmt:
  2042. ast_file_err(f, token, "You cannot a return statement");
  2043. break;
  2044. }
  2045. return make_defer_stmt(f, token, statement);
  2046. }
  2047. AstNode *parse_asm_stmt(AstFile *f) {
  2048. Token token = expect_token(f, Token_asm);
  2049. b32 is_volatile = false;
  2050. if (allow_token(f, Token_volatile)) {
  2051. is_volatile = true;
  2052. }
  2053. Token open, close, code_string;
  2054. open = expect_token(f, Token_OpenBrace);
  2055. code_string = expect_token(f, Token_String);
  2056. AstNode *output_list = NULL;
  2057. AstNode *input_list = NULL;
  2058. AstNode *clobber_list = NULL;
  2059. isize output_count = 0;
  2060. isize input_count = 0;
  2061. isize clobber_count = 0;
  2062. // TODO(bill): Finish asm statement and determine syntax
  2063. // if (f->cursor[0].kind != Token_CloseBrace) {
  2064. // expect_token(f, Token_Colon);
  2065. // }
  2066. close = expect_token(f, Token_CloseBrace);
  2067. return make_asm_stmt(f, token, is_volatile, open, close, code_string,
  2068. output_list, input_list, clobber_list,
  2069. output_count, input_count, clobber_count);
  2070. }
  2071. AstNode *parse_stmt(AstFile *f) {
  2072. AstNode *s = NULL;
  2073. Token token = f->cursor[0];
  2074. switch (token.kind) {
  2075. // Operands
  2076. case Token_Identifier:
  2077. case Token_Integer:
  2078. case Token_Float:
  2079. case Token_Rune:
  2080. case Token_String:
  2081. case Token_OpenParen:
  2082. // Unary Operators
  2083. case Token_Add:
  2084. case Token_Sub:
  2085. case Token_Xor:
  2086. case Token_Not:
  2087. s = parse_simple_stmt(f);
  2088. expect_semicolon_after_stmt(f, s);
  2089. return s;
  2090. // TODO(bill): other keywords
  2091. case Token_if: return parse_if_stmt(f);
  2092. case Token_return: return parse_return_stmt(f);
  2093. case Token_for: return parse_for_stmt(f);
  2094. case Token_match: return parse_match_stmt(f);
  2095. case Token_defer: return parse_defer_stmt(f);
  2096. case Token_asm: return parse_asm_stmt(f);
  2097. case Token_break:
  2098. case Token_continue:
  2099. case Token_fallthrough:
  2100. next_token(f);
  2101. s = make_branch_stmt(f, token);
  2102. expect_semicolon_after_stmt(f, s);
  2103. return s;
  2104. case Token_using: {
  2105. AstNode *node = NULL;
  2106. next_token(f);
  2107. node = parse_stmt(f);
  2108. b32 valid = false;
  2109. switch (node->kind) {
  2110. case AstNode_ExprStmt: {
  2111. AstNode *e = unparen_expr(node->ExprStmt.expr);
  2112. while (e->kind == AstNode_SelectorExpr) {
  2113. e = unparen_expr(e->SelectorExpr.selector);
  2114. }
  2115. if (e->kind == AstNode_Ident) {
  2116. valid = true;
  2117. }
  2118. } break;
  2119. case AstNode_VarDecl:
  2120. if (node->VarDecl.kind == Declaration_Mutable) {
  2121. valid = true;
  2122. }
  2123. break;
  2124. }
  2125. if (!valid) {
  2126. ast_file_err(f, token, "Illegal use of `using` statement.");
  2127. return make_bad_stmt(f, token, f->cursor[0]);
  2128. }
  2129. return make_using_stmt(f, token, node);
  2130. } break;
  2131. case Token_Hash: {
  2132. s = parse_tag_stmt(f, NULL);
  2133. if (are_strings_equal(s->TagStmt.name.string, make_string("load"))) {
  2134. Token file_path = expect_token(f, Token_String);
  2135. if (f->curr_proc == NULL) {
  2136. return make_load_decl(f, s->TagStmt.token, file_path);
  2137. }
  2138. ast_file_err(f, token, "You cannot `load` within a procedure. This must be done at the file scope.");
  2139. return make_bad_decl(f, token, file_path);
  2140. } else if (are_strings_equal(s->TagStmt.name.string, make_string("foreign_system_library"))) {
  2141. Token file_path = expect_token(f, Token_String);
  2142. if (f->curr_proc == NULL) {
  2143. return make_foreign_system_library(f, s->TagStmt.token, file_path);
  2144. }
  2145. ast_file_err(f, token, "You cannot using `foreign_system_library` within a procedure. This must be done at the file scope.");
  2146. return make_bad_decl(f, token, file_path);
  2147. } else if (are_strings_equal(s->TagStmt.name.string, make_string("thread_local"))) {
  2148. AstNode *var_decl = parse_simple_stmt(f);
  2149. if (var_decl->kind != AstNode_VarDecl ||
  2150. var_decl->VarDecl.kind != Declaration_Mutable) {
  2151. ast_file_err(f, token, "#thread_local may only be applied to variable declarations");
  2152. return make_bad_decl(f, token, ast_node_token(var_decl));
  2153. }
  2154. if (f->curr_proc != NULL) {
  2155. ast_file_err(f, token, "#thread_local is only allowed at the file scope.");
  2156. return make_bad_decl(f, token, ast_node_token(var_decl));
  2157. }
  2158. var_decl->VarDecl.tags |= VarDeclTag_thread_local;
  2159. return var_decl;
  2160. }
  2161. s->TagStmt.stmt = parse_stmt(f); // TODO(bill): Find out why this doesn't work as an argument
  2162. return s;
  2163. } break;
  2164. case Token_OpenBrace: return parse_block_stmt(f);
  2165. case Token_Semicolon:
  2166. s = make_empty_stmt(f, token);
  2167. next_token(f);
  2168. return s;
  2169. }
  2170. ast_file_err(f, token,
  2171. "Expected a statement, got `%.*s`",
  2172. LIT(token_strings[token.kind]));
  2173. fix_advance_to_next_stmt(f);
  2174. return make_bad_stmt(f, token, f->cursor[0]);
  2175. }
  2176. AstNode *parse_stmt_list(AstFile *f, isize *list_count_) {
  2177. AstNode *list_root = NULL;
  2178. AstNode *list_curr = NULL;
  2179. isize list_count = 0;
  2180. while (f->cursor[0].kind != Token_case &&
  2181. f->cursor[0].kind != Token_default &&
  2182. f->cursor[0].kind != Token_CloseBrace &&
  2183. f->cursor[0].kind != Token_EOF) {
  2184. AstNode *stmt = parse_stmt(f);
  2185. if (stmt && stmt->kind != AstNode_EmptyStmt) {
  2186. DLIST_APPEND(list_root, list_curr, stmt);
  2187. list_count++;
  2188. }
  2189. }
  2190. if (list_count_) *list_count_ = list_count;
  2191. return list_root;
  2192. }
  2193. ParseFileError init_ast_file(AstFile *f, String fullpath) {
  2194. if (!string_has_extension(fullpath, make_string("odin"))) {
  2195. gb_printf_err("Only `.odin` files are allowed\n");
  2196. return ParseFile_WrongExtension;
  2197. }
  2198. TokenizerInitError err = init_tokenizer(&f->tokenizer, fullpath);
  2199. if (err == TokenizerInit_None) {
  2200. gb_array_init(f->tokens, gb_heap_allocator());
  2201. for (;;) {
  2202. Token token = tokenizer_get_token(&f->tokenizer);
  2203. if (token.kind == Token_Invalid)
  2204. return ParseFile_InvalidToken;
  2205. gb_array_append(f->tokens, token);
  2206. if (token.kind == Token_EOF)
  2207. break;
  2208. }
  2209. f->cursor = &f->tokens[0];
  2210. // NOTE(bill): Is this big enough or too small?
  2211. isize arena_size = gb_size_of(AstNode);
  2212. arena_size *= 2*gb_array_count(f->tokens);
  2213. gb_arena_init_from_allocator(&f->arena, gb_heap_allocator(), arena_size);
  2214. f->curr_proc = NULL;
  2215. return ParseFile_None;
  2216. }
  2217. switch (err) {
  2218. case TokenizerInit_NotExists:
  2219. return ParseFile_NotFound;
  2220. case TokenizerInit_Permission:
  2221. return ParseFile_Permission;
  2222. case TokenizerInit_Empty:
  2223. return ParseFile_EmptyFile;
  2224. }
  2225. return ParseFile_InvalidFile;
  2226. }
  2227. void destroy_ast_file(AstFile *f) {
  2228. gb_arena_free(&f->arena);
  2229. gb_array_free(f->tokens);
  2230. gb_free(gb_heap_allocator(), f->tokenizer.fullpath.text);
  2231. destroy_tokenizer(&f->tokenizer);
  2232. }
  2233. b32 init_parser(Parser *p) {
  2234. gb_array_init(p->files, gb_heap_allocator());
  2235. gb_array_init(p->loads, gb_heap_allocator());
  2236. gb_array_init(p->libraries, gb_heap_allocator());
  2237. gb_array_init(p->system_libraries, gb_heap_allocator());
  2238. return true;
  2239. }
  2240. void destroy_parser(Parser *p) {
  2241. // TODO(bill): Fix memory leak
  2242. gb_for_array(i, p->files) {
  2243. destroy_ast_file(&p->files[i]);
  2244. }
  2245. #if 1
  2246. gb_for_array(i, p->loads) {
  2247. // gb_free(gb_heap_allocator(), p->loads[i].text);
  2248. }
  2249. #endif
  2250. gb_array_free(p->files);
  2251. gb_array_free(p->loads);
  2252. gb_array_free(p->libraries);
  2253. gb_array_free(p->system_libraries);
  2254. }
  2255. // NOTE(bill): Returns true if it's added
  2256. b32 try_add_load_path(Parser *p, String import_file) {
  2257. gb_for_array(i, p->loads) {
  2258. String import = p->loads[i];
  2259. if (are_strings_equal(import, import_file)) {
  2260. return false;
  2261. }
  2262. }
  2263. gb_array_append(p->loads, import_file);
  2264. return true;
  2265. }
  2266. // NOTE(bill): Returns true if it's added
  2267. b32 try_add_foreign_system_library_path(Parser *p, String import_file) {
  2268. gb_for_array(i, p->system_libraries) {
  2269. String import = p->system_libraries[i];
  2270. if (are_strings_equal(import, import_file)) {
  2271. return false;
  2272. }
  2273. }
  2274. gb_array_append(p->system_libraries, import_file);
  2275. return true;
  2276. }
  2277. gb_global Rune illegal_import_runes[] = {
  2278. '"', '\'', '`', ' ',
  2279. '\\', // NOTE(bill): Disallow windows style filepaths
  2280. '!', '$', '%', '^', '&', '*', '(', ')', '=', '+',
  2281. '[', ']', '{', '}',
  2282. ';', ':', '#',
  2283. '|', ',', '<', '>', '?',
  2284. };
  2285. b32 is_load_path_valid(String path) {
  2286. if (path.len > 0) {
  2287. u8 *start = path.text;
  2288. u8 *end = path.text + path.len;
  2289. u8 *curr = start;
  2290. Rune r = -1;
  2291. while (curr < end) {
  2292. isize width = 1;
  2293. r = curr[0];
  2294. if (r >= 0x80) {
  2295. width = gb_utf8_decode(curr, end-curr, &r);
  2296. if (r == GB_RUNE_INVALID && width == 1)
  2297. return false;
  2298. else if (r == GB_RUNE_BOM && curr-start > 0)
  2299. return false;
  2300. }
  2301. for (isize i = 0; i < gb_count_of(illegal_import_runes); i++) {
  2302. if (r == illegal_import_runes[i])
  2303. return false;
  2304. }
  2305. curr += width;
  2306. }
  2307. return true;
  2308. }
  2309. return false;
  2310. }
  2311. void parse_file(Parser *p, AstFile *f) {
  2312. String filepath = f->tokenizer.fullpath;
  2313. String base_dir = filepath;
  2314. for (isize i = filepath.len-1; i >= 0; i--) {
  2315. if (base_dir.text[i] == GB_PATH_SEPARATOR)
  2316. break;
  2317. base_dir.len--;
  2318. }
  2319. f->decls = parse_stmt_list(f, &f->decl_count);
  2320. for (AstNode *node = f->decls; node != NULL; node = node->next) {
  2321. if (!is_ast_node_decl(node) &&
  2322. node->kind != AstNode_BadStmt &&
  2323. node->kind != AstNode_EmptyStmt) {
  2324. // NOTE(bill): Sanity check
  2325. ast_file_err(f, ast_node_token(node), "Only declarations are allowed at file scope");
  2326. } else {
  2327. if (node->kind == AstNode_LoadDecl) {
  2328. auto *id = &node->LoadDecl;
  2329. String file_str = id->filepath.string;
  2330. if (!is_load_path_valid(file_str)) {
  2331. ast_file_err(f, ast_node_token(node), "Invalid `load` path");
  2332. continue;
  2333. }
  2334. isize str_len = base_dir.len+file_str.len;
  2335. u8 *str = gb_alloc_array(gb_heap_allocator(), u8, str_len+1);
  2336. defer (gb_free(gb_heap_allocator(), str));
  2337. gb_memcopy(str, base_dir.text, base_dir.len);
  2338. gb_memcopy(str+base_dir.len, file_str.text, file_str.len);
  2339. str[str_len] = '\0';
  2340. char *path_str = gb_path_get_full_name(gb_heap_allocator(), cast(char *)str);
  2341. String import_file = make_string(path_str);
  2342. if (!try_add_load_path(p, import_file)) {
  2343. gb_free(gb_heap_allocator(), import_file.text);
  2344. }
  2345. } else if (node->kind == AstNode_ForeignSystemLibrary) {
  2346. auto *id = &node->ForeignSystemLibrary;
  2347. String file_str = id->filepath.string;
  2348. if (!is_load_path_valid(file_str)) {
  2349. ast_file_err(f, ast_node_token(node), "Invalid `foreign_system_library` path");
  2350. continue;
  2351. }
  2352. try_add_foreign_system_library_path(p, file_str);
  2353. }
  2354. }
  2355. }
  2356. }
  2357. ParseFileError parse_files(Parser *p, char *init_filename) {
  2358. char *fullpath_str = gb_path_get_full_name(gb_heap_allocator(), init_filename);
  2359. String init_fullpath = make_string(fullpath_str);
  2360. gb_array_append(p->loads, init_fullpath);
  2361. p->init_fullpath = init_fullpath;
  2362. gb_for_array(i, p->loads) {
  2363. String import_path = p->loads[i];
  2364. AstFile file = {};
  2365. ParseFileError err = init_ast_file(&file, import_path);
  2366. if (err != ParseFile_None) {
  2367. gb_printf_err("Failed to parse file: %.*s\n", LIT(import_path));
  2368. switch (err) {
  2369. case ParseFile_WrongExtension:
  2370. gb_printf_err("\tInvalid file extension\n");
  2371. break;
  2372. case ParseFile_InvalidFile:
  2373. gb_printf_err("\tInvalid file\n");
  2374. break;
  2375. case ParseFile_EmptyFile:
  2376. gb_printf_err("\tFile is empty\n");
  2377. break;
  2378. case ParseFile_Permission:
  2379. gb_printf_err("\tFile permissions problem\n");
  2380. break;
  2381. case ParseFile_NotFound:
  2382. gb_printf_err("\tFile cannot be found\n");
  2383. break;
  2384. case ParseFile_InvalidToken:
  2385. gb_printf_err("\tInvalid token found in file\n");
  2386. break;
  2387. }
  2388. return err;
  2389. }
  2390. parse_file(p, &file);
  2391. gb_array_append(p->files, file);
  2392. p->total_token_count += gb_array_count(file.tokens);
  2393. }
  2394. return ParseFile_None;
  2395. }