12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199 |
- ' Copyright (c) 2013-2016 Bruce A Henderson
- '
- ' Based on the public domain Monkey "trans" by Mark Sibly
- '
- ' This software is provided 'as-is', without any express or implied
- ' warranty. In no event will the authors be held liable for any damages
- ' arising from the use of this software.
- '
- ' Permission is granted to anyone to use this software for any purpose,
- ' including commercial applications, and to alter it and redistribute it
- ' freely, subject to the following restrictions:
- '
- ' 1. The origin of this software must not be misrepresented; you must not
- ' claim that you wrote the original software. If you use this software
- ' in a product, an acknowledgment in the product documentation would be
- ' appreciated but is not required.
- '
- ' 2. Altered source versions must be plainly marked as such, and must not be
- ' misrepresented as being the original software.
- '
- ' 3. This notice may not be removed or altered from any source
- ' distribution.
- '
- SuperStrict
- Import BRL.MaxUtil
- Import "toker.bmx"
- Import "iparser.bmx"
- Global FILE_EXT$="bmx"
- Type TForEachinStmt Extends TLoopStmt
- Field varid$
- Field varty:TType
- Field varlocal:Int
- Field expr:TExpr
- Field block:TBlockDecl
- Field varExpr:TExpr
-
- Field stmts:TList=New TList
- Method Create:TForEachinStmt( varid$,varty:TType,varlocal:Int,expr:TExpr,block:TBlockDecl,loopLabel:TLoopLabelDecl,varExpr:TExpr )
- Self.varid=varid
- Self.varty=varty
- Self.varlocal=varlocal
- Self.expr=expr
- Self.block=block
- Self.loopLabel=loopLabel
- Self.varExpr = varExpr
- Return Self
- End Method
- Method OnCopy:TStmt( scope:TScopeDecl )
- Return New TForEachinStmt.Create( varid,varty,varlocal,expr.Copy(),block.CopyBlock( scope ),TLoopLabelDecl(loopLabel.Copy()), varExpr.Copy() )
- End Method
- Method OnSemant()
- expr=expr.Semant()
- If TArrayType( expr.exprType ) Or TStringType( expr.exprType )
- Local exprTmp:TLocalDecl=New TLocalDecl.Create( "",Null,expr,,True )
- Local indexTmp:TLocalDecl=New TLocalDecl.Create( "",Null,New TConstExpr.Create( New TUIntType,"0" ),,True )
- Local lenExpr:TExpr=New TIdentExpr.Create( "Length",New TVarExpr.Create( exprTmp ) )
- Local cmpExpr:TExpr=New TBinaryCompareExpr.Create( "<",New TVarExpr.Create( indexTmp ),lenExpr )
- Local indexExpr:TExpr=New TIndexExpr.Create( New TVarExpr.Create( exprTmp ),[New TVarExpr.Create( indexTmp )] )
- Local addExpr:TExpr=New TBinaryMathExpr.Create( "+",New TVarExpr.Create( indexTmp ),New TConstExpr.Create( New TIntType,"1" ) )
- Local cont:TContinueStmt
-
- If varlocal
- ' array of object ?
- If TArrayType( expr.exprType ) And TObjectType(TArrayType( expr.exprType ).elemType) And (Not TObjectType(TArrayType( expr.exprType ).elemType).classdecl.IsExtern() ..
- Or (TObjectType(TArrayType( expr.exprType ).elemType).classdecl.IsExtern() ..
- And IsPointerType(TArrayType( expr.exprType ).elemType))) Then
- Local cExpr:TExpr
-
- If TIdentType(varty) And TIdentType(varty).ident = "Object" Then
- cExpr = indexExpr
- Else
- cExpr = New TCastExpr.Create( varty, indexExpr,CAST_EXPLICIT )
- End If
- ' local variable
- Local varTmp:TLocalDecl=New TLocalDecl.Create( varid,varty,cExpr )
- ' local var as expression
- Local expr:TExpr=New TVarExpr.Create( varTmp )
- ' var = Null
- expr=New TBinaryCompareExpr.Create( "=",expr, New TNullExpr.Create(TType.nullObjectType))
- ' then continue
- Local thenBlock:TBlockDecl=New TBlockDecl.Create( block.scope )
- Local elseBlock:TBlockDecl=New TBlockDecl.Create( block.scope )
- cont = New TContinueStmt
- thenBlock.AddStmt cont
- block.stmts.AddFirst New TIfStmt.Create( expr,thenBlock,elseBlock )
- block.stmts.AddFirst New TAssignStmt.Create( "=",New TVarExpr.Create( indexTmp ),addExpr )
- block.stmts.AddFirst New TDeclStmt.Create( varTmp )
- Else
- Local varTmp:TLocalDecl=New TLocalDecl.Create( varid,varty,indexExpr )
- block.stmts.AddFirst New TAssignStmt.Create( "=",New TVarExpr.Create( indexTmp ),addExpr, True )
- block.stmts.AddFirst New TDeclStmt.Create( varTmp, True )
- End If
- Else
-
- If TArrayType( expr.exprType ) And TObjectType(TArrayType( expr.exprType ).elemType) Then
- ' var = Null
- If Not varty Then
- varExpr = varExpr.Semant()
- varty = varExpr.exprType
- 'Local decl:TValDecl = block.scope.FindValDecl(varid.ToLower())
-
- 'If decl Then
- ' decl.Semant()
- '
- ' varty = decl.ty.Copy()
- 'End If
- End If
- ' expr=New TBinaryCompareExpr.Create( "=",New TIdentExpr.Create( varid ), New TNullExpr.Create(TType.nullObjectType))
- expr=New TBinaryCompareExpr.Create( "=",varExpr, New TNullExpr.Create(TType.nullObjectType))
- ' then continue
- Local thenBlock:TBlockDecl=New TBlockDecl.Create( block.scope )
- Local elseBlock:TBlockDecl=New TBlockDecl.Create( block.scope )
- cont = New TContinueStmt
- thenBlock.AddStmt cont
- block.stmts.AddFirst New TIfStmt.Create( expr,thenBlock,elseBlock )
- 'block.stmts.AddFirst New TDeclStmt.Create( varTmp )
- block.stmts.AddFirst New TAssignStmt.Create( "=",New TVarExpr.Create( indexTmp ),addExpr, True )
- ' block.stmts.AddFirst New TAssignStmt.Create( "=",New TIdentExpr.Create( varid ),New TCastExpr.Create( varty, indexExpr,CAST_EXPLICIT ), True )
- block.stmts.AddFirst New TAssignStmt.Create( "=",varExpr,New TCastExpr.Create( varty, indexExpr,CAST_EXPLICIT ), True )
- Else
- block.stmts.AddFirst New TAssignStmt.Create( "=",New TVarExpr.Create( indexTmp ),addExpr, True )
- ' block.stmts.AddFirst New TAssignStmt.Create( "=",New TIdentExpr.Create( varid ),indexExpr, True )
- block.stmts.AddFirst New TAssignStmt.Create( "=",varExpr,indexExpr, True )
- End If
- EndIf
- Local whileStmt:TWhileStmt=New TWhileStmt.Create( cmpExpr,block,Null, True )
- block=New TBlockDecl.Create( block.scope, True )
- block.AddStmt New TDeclStmt.Create( exprTmp, True )
- block.AddStmt New TDeclStmt.Create( indexTmp, True )
- block.AddStmt whileStmt
-
- If cont Then
- cont.loop = whileStmt
- End If
- Else If TObjectType( expr.exprType )
- Local tmpDecl:TDeclStmt
- If TInvokeExpr(expr) Or TInvokeMemberExpr(expr) Then
- Local tmpVar:TLocalDecl=New TLocalDecl.Create( "",expr.exprType,expr,,True )
- tmpVar.Semant()
- tmpDecl = New TDeclStmt.Create( tmpVar, True )
- expr = New TVarExpr.Create( tmpVar )
- End If
- Local enumerInit:TExpr=New TFuncCallExpr.Create( New TIdentExpr.Create( "ObjectEnumerator",expr ) )
- Local enumerTmp:TLocalDecl=New TLocalDecl.Create( "",Null,enumerInit,,True )
- Local hasNextExpr:TExpr=New TFuncCallExpr.Create( New TIdentExpr.Create( "HasNext",New TVarExpr.Create( enumerTmp ) ) )
- Local nextObjExpr:TExpr=New TFuncCallExpr.Create( New TIdentExpr.Create( "NextObject",New TVarExpr.Create( enumerTmp ) ) )
- Local cont:TContinueStmt
-
- If varlocal
- ' Local varTmp:TLocalDecl=New TLocalDecl.Create( varid,varty,nextObjExpr )
- ' block.stmts.AddFirst New TDeclStmt.Create( varTmp )
- Local cExpr:TExpr
-
- If TIdentType(varty) And TIdentType(varty).ident = "Object" Then
- cExpr = nextObjExpr
- Else
- cExpr = New TCastExpr.Create( varty, nextObjExpr,CAST_EXPLICIT )
- End If
- ' local variable
- Local varTmp:TLocalDecl=New TLocalDecl.Create( varid,varty,cExpr)
- ' local var as expression
- Local expr:TExpr=New TVarExpr.Create( varTmp )
- ' var = Null
- expr=New TBinaryCompareExpr.Create( "=",expr, New TNullExpr.Create(TType.nullObjectType))
- ' then continue
- Local thenBlock:TBlockDecl=New TBlockDecl.Create( block.scope, True )
- Local elseBlock:TBlockDecl=New TBlockDecl.Create( block.scope, True )
- cont = New TContinueStmt.Create(Null, True)
- thenBlock.AddStmt cont
- block.stmts.AddFirst New TIfStmt.Create( expr,thenBlock,elseBlock, True )
- block.stmts.AddFirst New TDeclStmt.Create( varTmp, True )
- Else
- If Not varty Then
- varExpr = varExpr.Semant()
- varty = varExpr.exprType
- End If
-
- ' If Not varty Then
- ' Local decl:TValDecl = block.scope.FindValDecl(varid.ToLower())
- '
- ' If decl Then
- ' decl.Semant()
- '
- ' varty = decl.ty.Copy()
- ' End If
- ' End If
-
- ' var = Null
- ' Local expr:TExpr=New TBinaryCompareExpr.Create( "=",New TIdentExpr.Create( varid ), New TNullExpr.Create(TType.nullObjectType))
- Local expr:TExpr=New TBinaryCompareExpr.Create( "=",varExpr, New TNullExpr.Create(TType.nullObjectType))
- ' then continue
- Local thenBlock:TBlockDecl=New TBlockDecl.Create( block.scope )
- Local elseBlock:TBlockDecl=New TBlockDecl.Create( block.scope )
- cont = New TContinueStmt
- thenBlock.AddStmt cont
- block.stmts.AddFirst New TIfStmt.Create( expr,thenBlock,elseBlock )
- 'block.stmts.AddFirst New TDeclStmt.Create( varTmp )
- ' block.stmts.AddFirst New TAssignStmt.Create( "=",New TIdentExpr.Create( varid ),New TCastExpr.Create( varty, nextObjExpr,CAST_EXPLICIT ))
- block.stmts.AddFirst New TAssignStmt.Create( "=",varExpr,New TCastExpr.Create( varty, nextObjExpr,CAST_EXPLICIT ))
- EndIf
- Local whileStmt:TWhileStmt=New TWhileStmt.Create( hasNextExpr,block,Null, True )
- block=New TBlockDecl.Create( block.scope, True )
- If tmpDecl Then
- block.AddStmt tmpDecl
- End If
- block.AddStmt New TDeclStmt.Create( enumerTmp, True )
- block.AddStmt whileStmt
-
- If cont Then
- cont.loop = whileStmt
- End If
- Else
- InternalErr
- EndIf
- block.Semant
- End Method
- Method Trans$()
- _trans.EmitBlock block
- End Method
- End Type
- Type TIncbin
- Field file:String
- Field path:String
- Field id:Int
- Field length:Int
- Global count:Int
- Method Create:TIncbin(file:String, source:String)
- count :+ 1
- Self.file = file
- ' find the file
- If Not FileType(file) Then
- ' maybe relative to source
- Local dir:String = ExtractDir(source) + "/" + file
- If FileType(dir) = FILETYPE_FILE Then
- path = RealPath(dir)
- Else
- Return Null
- End If
- Else
- path = RealPath(file)
- End If
- id = count
- Return Self
- End Method
- End Type
- '***** Parser *****
- Type TParser
- Field _toker:TToker
- Field _toke:String
- Field _tokeType:Int
- 'Ronny: _tokerStack is unused
- 'Field _tokerStack:TList=New TList'<TToker>
- Field _block:TBlockDecl
- Field _blockStack:TList=New TList'<TBlockDecl>
- Field _errStack:TStringList=New TStringList
- Field _app:TAppDecl
- Field _module:TModuleDecl
- Field _externCasts:TMap = New TMap
- Method SetErr(toker:TToker = Null)
- Local t:TToker = _toker
- If toker Then
- t = toker
- End If
- If t.Path()
- _errInfo=FormatError(t.Path(),t.Line(),0)
- EndIf
- End Method
- Method DoErr(error:String, toker:TToker = Null)
- SetErr(toker)
- Err error
- End Method
- Method PushBlock( block:TBlockDecl )
- If _block <> Null Then
- _blockStack.AddLast _block
- End If
- _errStack.AddLast _errInfo
- _block=block
- End Method
- Method PopBlock()
- _block=TBlockDecl(_blockStack.RemoveLast())
- _errInfo=String(_errStack.RemoveLast())
- End Method
- Method RealPath$( path$ )
- Local popDir$=CurrentDir()
- ChangeDir ExtractDir( _toker.Path() )
- path=BRL.FileSystem.RealPath( path )
- ChangeDir popDir
- Return path
- End Method
- Method ActualPath:String(path:String)
- Local dir:String = ExtractDir(path)
- Local origFile:String = StripDir(path)
- Local lowerFile:String = origFile.ToLower()
-
- Local actualDir:String = ExtractDir(RealPath(path))
- Local files:String[] = LoadDir(actualDir)
- For Local file:String = EachIn files
- If file.ToLower() = lowerFile Then
- If file <> origFile Then
- ' we could raise as a warning instead, but an error encourages the user to fix their code ;-)
- Err "Actual file '" + file + "' differs in case with import '" + origFile + "'"
-
- ' what we might do were we to warn instead...
- If dir Then
- Return dir + "/" + file
- Else
- Return file
- End If
- End If
- Exit
- End If
- Next
- Return path
- End Method
-
- Method NextToke$()
- Local toke$=_toke
- Repeat
- _toke=_toker.NextToke()
- _tokeType=_toker.TokeType()
- Until _tokeType<>TOKE_SPACE
- If _tokeType=TOKE_KEYWORD _toke=_toker._tokeLower
- If toke="," SkipEols
- Return _toke
- End Method
- Method NextTokeToker$(toker:TToker)
- ' Local toke$=toker._toke
- Repeat
- toker.NextToke()
- Until toker.tokeType()<>TOKE_SPACE
- Return toker._toke
- End Method
- Method DescribeToke:String( toke:String )
- Select toke
- Case "~n"
- Return "end-of-line"
- End Select
- Return toke
- End Method
- Method CParse:Int( toke$ )
- If _toke.ToLower()<>toke
- Return False
- EndIf
- NextToke
- Return True
- End Method
- Method CParseToker:Int( toker:TToker, toke$ )
- If toker._toke.ToLower()<>toke
- Return False
- EndIf
- NextTokeToker(toker)
- Return True
- End Method
- Method Parse( toke$ )
- If Not CParse( toke )
- DoErr "Syntax error - expecting '"+toke+"'."
- EndIf
- End Method
- Method ParseToker( toker:TToker, toke$ )
- If Not CParseToker( toker, toke )
- DoErr "Syntax error - expecting '"+toke+"'.", toker
- EndIf
- End Method
- Method AtEos:Int()
- Return _toke="" Or _toke=";" Or _toke="~n" Or _toke="else"
- End Method
- Method SkipEols()
- While CParse( "~n" ) Or CParse(";")
- Wend
- SetErr
- End Method
- Method SkipEolsToker(toker:TToker)
- While CParseToker( toker, "~n" )
- Wend
- SetErr
- End Method
- Method ParseStringLit$()
- If _tokeType<>TOKE_STRINGLIT Err "Expecting string literal."
- Local str$=BmxUnquote( _toke )
- NextToke
- Return str
- End Method
- Method ParseIdent$()
- Select _toke
- Case "@" NextToke
- Case "string","object", "self"
- Default
- If _tokeType<>TOKE_IDENT Err "Syntax error - expecting identifier."
- End Select
- Local id$=_toke
- NextToke
- Return id
- End Method
- Method ParseIdentType:TIdentType()
- Local id$=ParseIdent()
- 'DebugLog "ParseIdentType : " + id
- If CParse( "." ) id:+"."+ParseIdent()
- If CParse( "." ) id:+"."+ParseIdent()
- Local args:TIdentType[]
- If CParse( "<" )
- Local nargs:Int
- Repeat
- Local arg:TIdentType=ParseIdentType()
- If args.Length=nargs args=args+ New TIdentType[10]
- args[nargs]=arg
- nargs:+1
- Until Not CParse(",")
- args=args[..nargs]
- Parse ">"
- EndIf
- Return New TIdentType.Create( id,args )
- End Method
- Method CParseIdentType:TIdentType( inner:Int=False )
- If _tokeType<>TOKE_IDENT Return Null
- Local id$=ParseIdent()
- While CParse( "." )
- If _tokeType<>TOKE_IDENT Return Null
- id:+"."+ParseIdent()
- Wend
- If Not CParse( "<" )
- If inner Return New TIdentType.Create( id,Null )
- Return Null
- EndIf
- Local args:TType[]
- Local nargs:Int
- Repeat
- Local arg:TType=CParsePrimitiveType()
- If Not arg
- arg=CParseIdentType( True )
- If Not arg Return Null
- EndIf
- While IsArrayDef()
- arg = ParseArrayType(arg)
- Wend
- ' While CParse( "[]" )
- ' arg=arg.ArrayOf()
- ' Wend
- args = args + [arg]
- nargs :+ 1
- Until Not CParse(",")
- If Not CParse( ">" ) Return Null
- Return New TIdentType.Create( id,args )
- End Method
- Method CParsePrimitiveType:TType()
- If CParse( "string" ) Return TType.stringType
- If CParse( "object" ) Return New TIdentType.Create( "brl.classes.object" )
- Local ty:TType
- If CParse( "short" )
- ty = New TShortType
- Else If CParse( "byte" )
- ty = New TByteType
- Else If CParse( "int" )
- ty = New TIntType
- Else If CParse( "uint" )
- ty = New TUIntType
- Else If CParse( "float" )
- ty = New TFloatType
- Else If CParse( "long" )
- ty = New TLongType
- Else If CParse( "ulong" )
- ty = New TULongType
- Else If CParse( "double" )
- ty = New TDoubleType
- Else If CParse( "size_t" )
- ty = New TSizeTType
- Else If CParse( "int128" ) Then
- If opt_arch <> "x64" Err "Intrinsic types only available on x64"
- ty = New TInt128Type
- Else If CParse( "float128" ) Then
- If opt_arch <> "x64" Err "Intrinsic types only available on x64"
- ty = New TFloat128Type
- Else If CParse( "double128" ) Then
- If opt_arch <> "x64" Err "Intrinsic types only available on x64"
- ty = New TDouble128Type
- Else If CParse( "float64" ) Then
- If opt_arch <> "x64" Err "Intrinsic types only available on x64"
- ty = New TFloat64Type
- Else If CParse( "wparam" ) Then
- If opt_platform <> "win32" Err "WParam types only available on Win32"
- ty = New TWParamType
- Else If CParse( "lparam" ) Then
- If opt_platform <> "win32" Err "LParam types only available on Win32"
- ty = New TLParamType
- End If
- While CParse("ptr")
- ty = TType.MapToPointerType(ty)
- Wend
- Return ty
- End Method
- Method CParsePrimitiveNumberType:TType()
- If CParse( "short" ) Return New TShortType
- If CParse( "byte" ) Return New TByteType
- If CParse( "int" ) Return New TIntType
- If CParse( "uint" ) Return New TUIntType
- If CParse( "float" ) Return New TFloatType
- If CParse( "long" ) Return New TLongType
- If CParse( "ulong" ) Return New TULongType
- If CParse( "double" ) Return New TDoubleType
- If CParse( "size_t" ) Return New TSizeTType
- If CParse( "int128" ) Then
- If opt_arch <> "x64" Err "Intrinsic types only available on x64"
- Return New TInt128Type
- End If
- If CParse( "float128" ) Then
- If opt_arch <> "x64" Err "Intrinsic types only available on x64"
- Return New TFloat128Type
- End If
- If CParse( "double128" ) Then
- If opt_arch <> "x64" Err "Intrinsic types only available on x64"
- Return New TDouble128Type
- End If
- If CParse( "float64" ) Then
- If opt_arch <> "x64" Err "Intrinsic types only available on x64"
- Return New TFloat64Type
- End If
- If CParse( "wparam" ) Then
- If opt_platform <> "win32" Err "WParam types only available on Win32"
- Return New TWParamType
- End If
- If CParse( "lparam" ) Then
- If opt_platform <> "win32" Err "LParam types only available on Win32"
- Return New TLParamType
- End If
- End Method
- Method ParseNewType:TType()
- If CParse( "void" ) Return New TVoidType
- If CParse( "short" ) Return New TShortType
- If CParse( "byte" ) Return New TByteType
- If CParse( "int" ) Return New TIntType
- If CParse( "uint" ) Return New TUIntType
- If CParse( "float" ) Return New TFloatType
- If CParse( "string" ) Return TType.stringType
- If CParse( "object" ) Return New TIdentType.Create( "brl.classes.object" )
- If CParse( "long" ) Return New TLongType
- If CParse( "ulong" ) Return New TULongType
- If CParse( "double" ) Return New TDoubleType
- If CParse( "size_t" ) Return New TSizeTType
- If CParse( "int128" ) Then
- If opt_arch <> "x64" Err "Intrinsic types only available on x64"
- Return New TInt128Type
- End If
- If CParse( "float128" ) Then
- If opt_arch <> "x64" Err "Intrinsic types only available on x64"
- Return New TFloat128Type
- End If
- If CParse( "double128" ) Then
- If opt_arch <> "x64" Err "Intrinsic types only available on x64"
- Return New TDouble128Type
- End If
- If CParse( "float64" ) Then
- If opt_arch <> "x64" Err "Intrinsic types only available on x64"
- Return New TFloat64Type
- End If
- If CParse( "wparam" ) Then
- If opt_platform <> "win32" Err "WParam types only available on Win32"
- Return New TWParamType
- End If
- If CParse( "lparam" ) Then
- If opt_platform <> "win32" Err "LParam types only available on Win32"
- Return New TLParamType
- End If
- Return ParseIdentType()
- End Method
- Method ParseType:TType()
- Local ty:TType=CParsePrimitiveType()
- If ty Return ty
- Return ParseIdentType()
- End Method
- Method ParseConstNumberType:TType()
- Local ty:TType
- Select _toke
- Case "@"
- NextToke
- ty=New TByteType
- While CParse("ptr")
- ty = TType.MapToPointerType(ty)
- Wend
- Case "@@"
- NextToke
- ty=New TShortType
- While CParse("ptr")
- ty = TType.MapToPointerType(ty)
- Wend
- Case "%"
- NextToke
- ty=New TIntType
- While CParse("ptr")
- ty = TType.MapToPointerType(ty)
- Wend
- Case "#"
- NextToke
- ty=New TFloatType
- While CParse("ptr")
- ty = TType.MapToPointerType(ty)
- Wend
- Case "$"
- NextToke
- ty=New TStringType
- Case "!"
- NextToke
- ty=New TDoubleType
- While CParse("ptr")
- ty = TType.MapToPointerType(ty)
- Wend
- Case "%%"
- NextToke
- ty=New TLongType
- While CParse("ptr")
- ty = TType.MapToPointerType(ty)
- Wend
- Case ":"
- NextToke
- ty=CParsePrimitiveNumberType()
- If Not ty Then
- If CParse("string") Then
- ty=New TStringType
- Else
- ty = ParseIdentType()
- End If
- Else
- While CParse("ptr")
- ty = TType.MapToPointerType(ty)
- Wend
- End If
- End Select
- While IsArrayDef()
- ty = ParseArrayType(ty)
- Wend
- 'While CParse( "[]" )
- ' ty=New TArrayType.Create( ty )
- 'Wend
- Return ty
- End Method
- Method ParseDeclType:TType()
- Local ty:TType
- Select _toke
- Case "@"
- NextToke
- ty=New TByteType
- While CParse("ptr")
- ty = TType.MapToPointerType(ty)
- Wend
- Case "@@"
- NextToke
- ty=New TShortType
- While CParse("ptr")
- ty = TType.MapToPointerType(ty)
- Wend
- Case "%"
- NextToke
- ty=New TIntType
- While CParse("ptr")
- ty = TType.MapToPointerType(ty)
- Wend
- Case "%%"
- NextToke
- ty=New TLongType
- While CParse("ptr")
- ty = TType.MapToPointerType(ty)
- Wend
- Case "#"
- NextToke
- ty=New TFloatType
- If CParse("ptr") Then
- ty = TType.MapToPointerType(ty)
- End If
- Case "$"
- NextToke
- ty=New TStringType
- If CParse("z") Then
- ty._flags :| TType.T_CHAR_PTR
- Else If CParse("w") Then
- ty._flags :| TType.T_SHORT_PTR
- End If
- Case "!"
- NextToke
- ty=New TDoubleType
- While CParse("ptr")
- ty = TType.MapToPointerType(ty)
- Wend
- Case ":"
- NextToke
- ty=ParseType()
- If CParse("ptr") Then
-
- ' FIXME #200
- 'If TStringType(ty) = Null And (TObjectType(ty) = Null Or (TObjectType(ty) <> Null And TObjectType(ty).classDecl.IsExtern())) And TArrayType(ty) = Null Then
- ty = TType.MapToPointerType(ty)
-
- While CParse("ptr")
- ty = TType.MapToPointerType(ty)
- Wend
- 'Else
- ' ty = Null
- 'End If
- If Not ty DoErr "Invalid Pointer type."
- End If
- Case "("
- ' for Strict code, void will be converted to Int during semanting.
- ty=New TVoidType
- Default
- If _module.IsSuperStrict() Err "Illegal type expression."
- ty=New TIntType
- While CParse("ptr")
- ty = TType.MapToPointerType(ty)
- Wend
- End Select
-
- ' array ?
- While IsArrayDef()
- ty = ParseArrayType(ty)
- Wend
-
- Return ty
- End Method
- Method ParseArrayExpr:TArrayExpr()
- Parse "["
- Local args:TExpr[],nargs:Int
- Repeat
- Local arg:TExpr=ParseExpr()
- If args.Length=nargs args=args + New TExpr[10]
- args[nargs]=arg
- nargs:+1
- Until Not CParse(",")
- args=args[..nargs]
- Parse "]"
- Return New TArrayExpr.Create( args )
- End Method
- ' replaces While CParse( "[]" ) sections, with support for multi-dimension arrays
- Method ParseArrayType:TType(ty:TType)
- While True
- Local dims:Int = 1
-
- If CParse("[]") Then
- ty=New TArrayType.Create( ty )
- Exit
- End If
-
- If Not CParse("[") Then
- Exit
- End If
-
- While CParse( ",")
- dims :+ 1
- Wend
-
- Parse "]"
-
- ty=New TArrayType.Create( ty, dims )
- Exit
- Wend
- Return ty
- End Method
-
- Method IsArrayDef:Int()
- Local isDef:Int = True
- Local toker:TToker=New TToker.Copy(_toker)
- While True
- 'Local dims:Int = 1
-
- If CParseToker(toker, "[]") Then
- Exit
- End If
-
- If Not CParseToker(toker, "[") Then
- isDef = False
- Exit
- End If
-
- While CParseToker(toker, ",")
- 'dims :+ 1
- Wend
-
- If Not CParseToker(toker, "]") Then
- isDef = False
- Exit
- End If
- Exit
- Wend
- Return isDef
- End Method
- Method ParseArgs:TExpr[]( stmt:Int )
- Local args:TExpr[]
- If stmt
- If AtEos() Return args
- Else
- If _toke<>"(" Return args
- EndIf
- Local nargs:Int,eat:Int
- If _toke="("
- If stmt
- Local toker:TToker=New TToker.Copy(_toker),bra:Int=1
- Repeat
- toker.NextToke
- toker.SkipSpace
- Select toker.Toke().ToLower()
- Case "","else"
- Err "Parenthesis mismatch error."
- Case "(","["
- bra:+1
- Case "]",")"
- bra:-1
- If bra Continue
- toker.NextToke
- toker.SkipSpace
- Select toker.Toke().ToLower()
- Case ".","(","[","",";","~n","else"
- eat=True
- End Select
- Exit
- Case ","
- If bra<>1 Continue
- eat=True
- Exit
- End Select
- Forever
- Else
- eat=True
- EndIf
- If eat And NextToke()=")"
- NextToke
- Return args
- EndIf
- EndIf
- Repeat
- Local arg:TExpr
- If _toke And _toke<>"," arg=ParseExpr()
- If args.Length=nargs args=args + New TExpr[10]
- args[nargs]=arg
- nargs:+1
- Until Not CParse(",")
- args=args[..nargs]
- If eat Parse ")"
- Return args
- End Method
- Method ParsePrimaryExpr:TExpr( stmt:Int )
- Local expr:TExpr
- Select _toke.ToLower()
- Case "("
- NextToke
- expr=ParseExpr()
- Parse ")"
- Case "["
- expr=ParseArrayExpr()
- Case "[]"
- NextToke
- expr=New TConstExpr.Create( TType.emptyArrayType,"" )
- Case "."
- expr=New TScopeExpr.Create( _module )
- Case "new"
- NextToke
-
- If _toke = "(" Then
- ' call constructor
- expr=New TNewExpr.Create( ParseArgs(stmt) )
- Else
- Local ty:TType=ParseType()
-
- While CParse("ptr")
- ty = TType.MapToPointerType(ty)
- Wend
-
- If _toke = "[" Or _toke = "[]" Then
- Local depth:Int = 0
- Local ln:TExpr[]
- Local tmpTy:TType = ty.Copy()
-
- Repeat
- Local dims:Int = 1
-
- If CParse("[]") Then
- tmpTy=New TArrayType.Create( tmpTy )
- depth :+ 1
- Continue
- End If
-
- ' looking for an array with expression
- If Not ln Then
- Parse "["
- Else
- If Not CParse("[") Then
- Exit
- Else
- Err "Unexpected '[' after array size declaration"
- End If
- End If
-
- Repeat
- If CParse(",") Then
- dims :+ 1
- Continue
- End If
- If CParse("]") Exit
- ln = ln + [ParseExpr()]
- If CParse("]") Exit
- Parse(",")
- dims :+ 1
- Forever
-
- If Not ln Then
- tmpTy=New TArrayType.Create( tmpTy, dims )
- End If
- Forever
-
- If ln Then
- ty = tmpTy
- End If
-
- ' Repeat
- 'If CParse( "[" )
- ' Repeat
- ' ln = ln + [ParseExpr()]
- ' If CParse("]") Exit
- ' Parse ","
- ' Forever
- 'Parse "]"
- ' ty = ParseArrayType(ty)
- ' Forever
- 'While CParse( "[]" )
- ' ty=New TArrayType.Create( ty)
- 'Wend
- expr=New TNewArrayExpr.Create( ty,ln )
- Else
- expr=New TNewObjectExpr.Create( ty,ParseArgs( stmt ) )
- EndIf
- End If
- Case "null"
- NextToke
- expr = New TNullExpr.Create(TType.nullObjectType)
- 'expr=New TConstExpr.Create( TType.nullObjectType,"" )
- Case "true"
- NextToke
- expr=New TConstExpr.Create( New TIntType,"1" )
- Case "false"
- NextToke
- expr=New TConstExpr.Create( New TIntType,"" )
- Case "int","long","float","double","object","short","byte","size_t","uint","ulong","int128","float64","float128","double128","lparam","wparam"
- Local id$=_toke
- Local ty:TType=ParseType()
- If TIntType(ty) And id.ToLower() <> "int" Then
- Select id.ToLower()
- Case "byte"
- ty = New TByteType
- Case "short"
- ty = New TShortType
- Case "uint"
- ty = New TUIntType
- Case "long"
- ty = New TLongType
- Case "ulong"
- ty = New TULongType
- Case "float"
- ty = New TFloatType
- Case "double"
- ty = New TDoubleType
- Case "size_t"
- ty = New TSizeTType
- Case "int128"
- If opt_arch <> "x64" Err "Intrinsic types only available on x64"
- ty = New TInt128Type
- Case "float128"
- If opt_arch <> "x64" Err "Intrinsic types only available on x64"
- ty = New TFloat128Type
- Case "double128"
- If opt_arch <> "x64" Err "Intrinsic types only available on x64"
- ty = New TDouble128Type
- Case "float64"
- If opt_arch <> "x64" Err "Intrinsic types only available on x64"
- ty = New TFloat64Type
- Case "wparam"
- If opt_platform <> "win32" Err "WParam types only available on Win32"
- ty = New TWParamType
- Case "lparam"
- If opt_platform <> "win32" Err "LParam types only available on Win32"
- ty = New TLParamType
- End Select
- End If
- While CParse("ptr")
- ty = TType.MapToPointerType(ty)
- Wend
- ' array
- ty = ParseArrayType(ty)
- 'While CParse( "[]" )
- ' ty=New TArrayType.Create( ty)
- 'Wend
- ' optional brackets
- If CParse( "(" )
- expr=ParseExpr()
- Parse ")"
- expr=New TCastExpr.Create( ty,expr,CAST_EXPLICIT )
- Else
- expr=ParseExpr()
-
- If TBinaryExpr(expr) Then
- ' cast lhs and apply to rhs
- Local cexpr:TCastExpr=New TCastExpr.Create( ty,TBinaryExpr(expr).lhs,CAST_EXPLICIT )
- TBinaryExpr(expr).lhs = cexpr
- Else
- expr=New TCastExpr.Create( ty,expr,CAST_EXPLICIT )
- End If
- EndIf
- Case "sizeof"
- NextToke
- Local ty:TType = ParseConstNumberType()
- If ty Then
- If Not TIntType(ty) Then
- Err "Return type for 'SizeOf' must be Int"
- End If
- End If
- ' optional brackets
- If CParse( "(" )
- expr=ParseExpr()
- Parse ")"
- expr=New TSizeOfExpr.Create( expr )
- Else
- expr=ParseExpr()
- expr=New TSizeOfExpr.Create( expr )
- EndIf
- Case "len"
- NextToke
-
- Local ty:TType = ParseConstNumberType()
- If ty Then
- If Not TIntType(ty) Then
- Err "Return type for 'Len' must be Int"
- End If
- End If
- ' optional brackets
- If CParse( "(" )
- expr=ParseExpr()
- Parse ")"
- expr=New TLenExpr.Create( expr )
- Else
- expr=ParseExpr()
- expr=New TLenExpr.Create( expr )
- EndIf
- Case "abs"
- NextToke
- ' optional brackets
- If CParse( "(" )
- expr=ParseExpr()
- Parse ")"
- expr=New TAbsExpr.Create( expr )
- Else
- expr=ParseExpr()
- expr=New TAbsExpr.Create( expr )
- EndIf
- Case "min"
- NextToke
- ' optional brackets
- Local b:Int = CParse( "(" )
- expr=ParseExpr()
- Parse ","
- Local expr2:TExpr=ParseExpr()
- If b Then
- Parse ")"
- End If
- expr=New TMinExpr.Create( expr, expr2 )
- Case "max"
- NextToke
- ' optional brackets
- Local b:Int = CParse( "(" )
- expr=ParseExpr()
- Parse ","
- Local expr2:TExpr=ParseExpr()
- If b Then
- Parse ")"
- End If
- expr=New TMaxExpr.Create( expr, expr2 )
- Case "asc"
- NextToke
- Local ty:TType = ParseConstNumberType()
- If ty Then
- If Not TIntType(ty) Then
- Err "Return type for 'Asc' must be Int"
- End If
- End If
-
- ' optional brackets
- If CParse( "(" )
- expr=ParseExpr()
- Parse ")"
- expr=New TAscExpr.Create( expr )
- Else
- expr=ParseExpr()
- expr=New TAscExpr.Create( expr )
- EndIf
- Case "chr"
- NextToke
- Local ty:TType = ParseConstNumberType()
- If ty Then
- If Not TStringType(ty) Then
- Err "Return type for 'Chr' must be String"
- End If
- End If
-
- ' optional brackets
- If CParse( "(" )
- expr=ParseExpr()
- Parse ")"
- expr=New TChrExpr.Create( expr )
- Else
- expr=ParseExpr()
- expr=New TChrExpr.Create( expr )
- EndIf
- Case "sgn"
- NextToke
- ' optional brackets
- If CParse( "(" )
- expr=ParseExpr()
- Parse ")"
- expr=New TSgnExpr.Create( expr )
- Else
- expr=ParseExpr()
- expr=New TSgnExpr.Create( expr )
- EndIf
- Case "string"
- Local id$=_toke
- Local ty:TType=ParseType()
- If CParse("ptr") Then
- ty = TType.MapToPointerType(ty)
- End If
- ' string array
- ty = ParseArrayType(ty)
- 'While CParse( "[]" )
- ' ty=New TArrayType.Create( ty)
- 'Wend
- If CParse( "(" )
- expr=ParseExpr()
- Parse ")"
- expr=New TCastExpr.Create( ty,expr,CAST_EXPLICIT )
- Else
- expr=New TIdentExpr.Create( id )
- EndIf
- Case "varptr"
- NextToke
- expr=ParseExpr()
- expr=New TCastExpr.Create( New TVarPtrType, expr, CAST_EXPLICIT )
- Case "pi"
- NextToke
- expr=New TConstExpr.Create( New TDoubleType, Pi )
- Case "self"
- NextToke
- expr=New TSelfExpr
- Case "super"
- NextToke
- Parse "."
- If _toke="new"
- Err "Call to super class constructor must be first statement in a constructor."
- EndIf
- Local id$=ParseIdent()
-
- ' eat any type stuff
- ParseConstNumberType()
- expr=New TInvokeSuperExpr.Create( id,ParseArgs( stmt ) )
- Default
- Select _tokeType
- Case TOKE_IDENT
- Local tok:TToker=New TToker.Copy( _toker )
- Local ty:TType=CParseIdentType()
- If ty
- expr=New TIdentTypeExpr.Create( ty )
- Else
- _toker=tok
- _toke=_toker.Toke()
- _tokeType=_toker.TokeType()
- expr=New TIdentExpr.Create( ParseIdent() )
- ty = ParseConstNumberType()
-
- If TArrayType(ty) Then
- If Not TArrayType(ty).elemType Then
- TArrayType(ty).elemType = New TIdentType.Create(TIdentExpr(expr).ident)
- expr=New TIdentTypeExpr.Create( ty )
- End If
- End If
- EndIf
- 'expr=New TIdentExpr.Create( ParseIdent() )
- Case TOKE_INTLIT
- expr=New TConstExpr.Create( New TIntType,_toke )
- NextToke
- Local ty:TType = ParseConstNumberType()
- If ty Then
- TConstExpr(expr).UpdateType(ty)
- End If
- Case TOKE_LONGLIT
- expr=New TConstExpr.Create( New TLongType,_toke )
- NextToke
-
- Local ty:TType = ParseConstNumberType()
- If ty Then
- TConstExpr(expr).UpdateType(ty)
- End If
- Case TOKE_FLOATLIT
- expr=New TConstExpr.Create( New TFloatType,_toke )
- NextToke
- Local ty:TType = ParseConstNumberType()
- If ty Then
- TConstExpr(expr).ty = ty
- End If
- Case TOKE_STRINGLIT
- expr=New TConstExpr.Create( TType.stringType,BmxUnquote( _toke ) )
- _app.mapStringConsts(BmxUnquote( _toke ))
- NextToke
- Default
- Err "Expecting expression but encountered '"+_toke+"'"
- End Select
- End Select
- Repeat
- Select _toke
- Case "."
- NextToke
-
- expr=New TIdentExpr.Create( ParseIdent(),expr )
-
- ParseConstNumberType()
- 'DebugLog expr.ToString()
- Case "("
- If expr = Null Then
- NextToke
- expr=ParseExpr()
- Parse ")"
- Else
- expr=New TFuncCallExpr.Create( expr,ParseArgs( stmt ) )
- End If
- Case "["
- NextToke
- If CParse( ".." )
- If _toke="]"
- expr=New TSliceExpr.Create( expr,Null,Null )
- Else
- expr=New TSliceExpr.Create( expr,Null,ParseExpr() )
- EndIf
- Parse "]"
- Else
- Local from:TExpr=ParseExpr()
- If CParse( ".." )
- If _toke="]"
- expr=New TSliceExpr.Create( expr,from,Null )
- Else
- expr=New TSliceExpr.Create( expr,from,ParseExpr() )
- EndIf
- Parse "]"
- Else
- Local ind:TExpr[] = [from]
- Repeat
- If CParse("]") Then
- Exit
- End If
- Parse ","
- ind = ind + [ParseExpr()]
- Forever
- expr=New TIndexExpr.Create( expr,ind )
- EndIf
- EndIf
- Default
- Return expr
- End Select
- Forever
- End Method
- Method ParseUnaryExpr:TExpr()
- SkipEols
- Local op$=_toke
- Select op
- Case "+","-","~~","not"
- NextToke
- Local expr:TExpr=ParseUnaryExpr()
- Return New TUnaryExpr.Create( op,expr )
- End Select
- Return ParsePrimaryExpr( False )
- End Method
- Method ParsePowExpr:TExpr()
- Local expr:TExpr=ParseUnaryExpr()
- Repeat
- Local op$=_toke
- Select op
- Case "^"
- NextToke
- Local rhs:TExpr=ParseUnaryExpr()
- expr=New TBinaryMathExpr.Create( op,expr,rhs )
- Default
- Return expr
- End Select
- Forever
- End Method
- Method ParseMulDivExpr:TExpr()
- Local expr:TExpr=ParsePowExpr()
- Repeat
- Local op$=_toke
- Select op
- Case "*","/","mod","shl","shr", "sar"
- NextToke
- Local rhs:TExpr=ParsePowExpr()
- expr=New TBinaryMathExpr.Create( op,expr,rhs )
- Default
- Return expr
- End Select
- Forever
- End Method
- Method ParseAddSubExpr:TExpr()
- Local expr:TExpr=ParseMulDivExpr()
- Repeat
- Local op$=_toke
- Select op
- Case "+","-"
- NextToke
- Local rhs:TExpr=ParseMulDivExpr()
- expr=New TBinaryMathExpr.Create( op,expr,rhs )
- Default
- Return expr
- End Select
- Forever
- End Method
- Method ParseBitandExpr:TExpr()
- Local expr:TExpr=ParseAddSubExpr()
- Repeat
- Local op$=_toke
- Select op
- Case "&","~~"
- NextToke
- Local rhs:TExpr=ParseAddSubExpr()
- expr=New TBinaryMathExpr.Create( op,expr,rhs )
- Default
- Return expr
- End Select
- Forever
- End Method
- Method ParseBitorExpr:TExpr()
- Local expr:TExpr=ParseBitandExpr()
- Repeat
- Local op$=_toke
- Select op
- Case "|"
- NextToke
- Local rhs:TExpr=ParseBitandExpr()
- expr=New TBinaryMathExpr.Create( op,expr,rhs )
- Default
- Return expr
- End Select
- Forever
- End Method
- Method ParseCompareExpr:TExpr()
- Local expr:TExpr=ParseBitorExpr()
- Repeat
- Local op$=_toke
- Select op
- Case "=","<",">","<=","=<",">=","=>","<>"
- NextToke
- ' <= or =>
- If (op=">" And (_toke="=")) Or (op="=" And (_toke=">"))
- op:+_toke
- NextToke
- ' <> or <= or =<
- Else If (op="<" And _toke=">") Or (op="<" And _toke="=") Or (op="=" And _toke="<")
- op:+_toke
- NextToke
- EndIf
- Local rhs:TExpr=ParseBitorExpr()
- expr=New TBinaryCompareExpr.Create( op,expr,rhs )
- Default
- Return expr
- End Select
- Forever
- End Method
- Method ParseAndExpr:TExpr()
- Local expr:TExpr=ParseCompareExpr()
- Repeat
- Local op$=_toke
- If op="and"
- NextToke
- Local rhs:TExpr=ParseCompareExpr()
- expr=New TBinaryLogicExpr.Create( op,expr,rhs )
- Else
- Return expr
- EndIf
- Forever
- End Method
- Method ParseOrExpr:TExpr()
- Local expr:TExpr=ParseAndExpr()
- Repeat
- Local op$=_toke
- If op="or"
- NextToke
- Local rhs:TExpr=ParseAndExpr()
- expr=New TBinaryLogicExpr.Create( op,expr,rhs )
- Else
- Return expr
- EndIf
- Forever
- End Method
- Method ParseExpr:TExpr()
- Return ParseOrExpr()
- End Method
- Rem
- unused atm
- Method ReadTillNextToken:string(amount:int=1)
- 'copy current toker and move one token forward
- local tok:TToker = New TToker.Copy(_toker)
- local result:string = _toker._toke
- for local i:int = 0 until amount
- NextTokeToker(tok)
- result :+ " "+ tok._toke
- Next
- return _toker._toke+" "+tok._toke
- End Method
- End Rem
-
- Method ParseIfStmt( term$, elseIfEndIfReadAheadCheck:Int = False )
- Local tok:TToker
- 'rules:
- '- the command "end" cannot be used as condition
- '- "endif" or "end if" is not allowed in singleline-ifs
- 'if current toke is "if", move on to the next toke
- CParse "if"
- 'read in the expression/condition following after "if"
- Local expr:TExpr=ParseExpr()
- 'if current toke is "then", move to next, else stay at this
- 'position -> makes "then" usage voluntary
- CParse "then"
- 'create empty blocks for then/else
- Local thenBlock:TBlockDecl=New TBlockDecl.Create( _block )
- Local elseBlock:TBlockDecl=New TBlockDecl.Create( _block )
- 'define if the current if is a "singleline if"
- '"singleline ifs" are not allowed to contain "endif" "end if"
- Local singleLineIf:Int = True
- 'to know if it is a multiline or singleline if we have to check
- 'for certain situations
- Select _toke
- Case "~n"
- 'if a <- newline
- ' print "a"
- 'endif
- singleLineIf = False
- Case "if"
- 'another "if" means the outer one is a singleline if!
- singleLineIf = True
- Case "else"
- 'if ReadTillNextToken().toLower() = "else if"
- ' print "IF: found if X then Y else if ..."
- 'else
- ' print "IF: found if X then Y else ..."
- 'endif
- 'also read "else if"
- singleLineIf = True
- Case "elseif"
- singleLineIf = True
- End Select
- 'set thenBlock as the active block
- PushBlock( thenBlock )
- 'now check each toke until we reach our desired term
- 'for singleline-if this is "~n", for multiline-if this is
- '"endif" or "end if"
- If singleLineIf
- term = "~n"
- Else
- term = "end" 'endif, end if
- EndIf
- 'only read until reaching the limit - or no valid toke was returned
- While _toke <> term
- Local currentToke:String = _toke
- Select currentToke
- 'file end before endif/end/elseif
- Case ""
- Err("Expecting expression but encountered end-of-file")
- '"endif" / "end if"
- Case "endif", "end"
- NextToke()
-
- If singleLineIf Then
- 'check for "end"-command ("if a=1 end")
- If currentToke = "end" And (currentToke + _toke) <> "endif" Then
- ParseEndStmt(False)
- 'found "end if"
- Else
- Err "'End If' without matching 'If'"
- Exit
- EndIf
- EndIf
-
- 'If currentToke = "endif" or (currentToke + _toke)="endif"
- ' 'do something if "endif/end if" happens ?
- 'Endif
- 'finish this if-statement
- Exit
- '"else" and "elseif" / "else if"
- Case "else","elseif"
- ' print "parsing "+currentToke
- If _block = elseBlock
- Err("If statement can only have one 'else' block.")
- EndIf
- 'switch from thenBlock to elseBlock
- PopBlock()
- PushBlock(elseBlock)
- 'move to next token, might contain "if" for "else if"
- 'doing it this way avoids to parse "elseif if" as
- 'else-statement
- NextToke()
- If currentToke = "elseif" Or (currentToke + _toke)="elseif"
- 'create a new if-statement and exit current handling
- ParseIfStmt(term, True)
- Exit
- EndIf
-
- Default
- 'parse the current and next tokens
- ParseStmt()
- currentToke = _toke
- 'handle the end-function and "end if"
- Select currentToke
- Case "end"
- 'check next toke too
- NextToke()
- 'found end-function
- If currentToke = "end" And (currentToke + _toke)<>"endif"
- ' print " parsing end .... handling"
- ParseEndStmt(False)
- 'found "end if"
- Else
- If CParse("if") Then
- If singleLineIf Then
- Err "'End If' without matching 'If'"
- End If
-
- Exit
- End If
-
- 'NextToke()
- EndIf
- End Select
- End Select
- Wend
-
- 'change block
- PopBlock()
- 'create a if-then[-else]-statement
- Local stmt:TIfStmt=New TIfStmt.Create( expr,thenBlock,elseBlock )
- _block.AddStmt stmt
- End Method
- Method ParseWhileStmt(loopLabel:TLoopLabelDecl = Null)
- Parse "while"
- Local expr:TExpr=ParseExpr()
- Local block:TBlockDecl=New TBlockDecl.Create( _block )
- PushBlock block
- While Not CParse( "wend" ) And Not CParse( "endwhile" )
- ' If CParse( "end" )
- ' CParse "while"
- ' Exit
- ' EndIf
- ParseStmt
- ' to handle "end" statement
- If _toke = "end" Then
- NextToke
- If _toke = "while" Then
- NextToke
- Exit
- Else
- ParseEndStmt(False)
- End If
- End If
- Wend
- PopBlock
- Local stmt:TWhileStmt=New TWhileStmt.Create( expr,block,loopLabel )
- _block.AddStmt stmt
- End Method
- Method ParseRepeatStmt(loopLabel:TLoopLabelDecl = Null)
- Parse "repeat"
- Local block:TBlockDecl=New TBlockDecl.Create( _block )
- PushBlock block
- While _toke<>"until" And _toke<>"forever"
- ParseStmt
- Wend
- PopBlock
- SetErr
- Local expr:TExpr
- If CParse( "until" )
- expr=ParseExpr()
- Else
- Parse "forever"
- expr=New TConstExpr.Create( New TBoolType,"" )
- EndIf
- Local stmt:TRepeatStmt=New TRepeatStmt.Create( block,expr,loopLabel )
- _block.AddStmt stmt
- End Method
- Method ParseForStmt(loopLabel:TLoopLabelDecl = Null)
- Parse "for"
- Local varid$,varty:TType,varlocal:Int
- Local varExpr:TExpr
- If CParse( "local" )
- varlocal=True
- varid=ParseIdent()
- 'If Not CParse( ":=" )
- varty=ParseDeclType()
- If varty._flags & (TType.T_CHAR_PTR | TType.T_SHORT_PTR) Then
- DoErr "Illegal variable type"
- End If
- If _toke = "(" Then
- Local fdecl:TFuncDecl = ParseFuncDecl("", FUNC_PTR | DECL_ARG)
- If Not varty Then
- varty = New TFunctionPtrType
- TFunctionPtrType(varty).func = fdecl
- Else
- fdecl.retType = varty
- varty = New TFunctionPtrType
- TFunctionPtrType(varty).func = fdecl
- End If
- TFunctionPtrType(varty).func.ident = varid
- ' function pointer array ?
- While IsArrayDef()
- varty = ParseArrayType(varty)
- Wend
- End If
-
- Parse( "=" )
- 'EndIf
- Else
- varlocal=False
-
- varExpr=ParsePrimaryExpr( False )
-
- 'varExpr = New TIdentExpr.Create( ParseIdent(),varExpr )
- 'ParseConstNumberType()
-
- ' varid=ParseIdent()
- 'While Cparse(".")
- 'NextToke
-
- ' varExpr = New TIdentExpr.Create( ParseIdent(),varExpr )
-
- ' ParseConstNumberType()
- 'Wend
- ' eat any type stuff
- ' ParseConstNumberType()
- Parse "="
- EndIf
- If CParse( "eachin" )
- Local expr:TExpr=ParseExpr()
- Local block:TBlockDecl=New TBlockDecl.Create( _block )
- PushBlock block
- While Not CParse( "next" )
- 'If CParse( "end" )
- ' CParse "for"
- ' Exit
- 'EndIf
- ParseStmt
- Wend
- PopBlock
- Local stmt:TForEachinStmt=New TForEachinStmt.Create( varid,varty,varlocal,expr,block,loopLabel, varExpr )
- _block.AddStmt stmt
- Return
- EndIf
- Local from:TExpr=ParseExpr()
- Local op$
- If CParse( "to" )
- op="<="
- Else If CParse( "until" )
- op="<"
- Else
- Err "Expecting 'To' or 'Until'."
- EndIf
- Local term:TExpr=ParseExpr()
- Local stp:TExpr
- If CParse( "step" )
- stp=ParseExpr()
- Else
- stp=New TConstExpr.Create( New TIntType,"1" )
- EndIf
- Local init:TStmt,expr:TExpr,incr:TStmt
- If varlocal
- Local indexVar:TLocalDecl=New TLocalDecl.Create( varid,varty,New TCastExpr.Create( varty,from,1 ),0 )
- init=New TDeclStmt.Create( indexVar )
- expr=New TBinaryCompareExpr.Create( op,New TVarExpr.Create( indexVar ),New TCastExpr.Create( varty,term,1 ) )
- incr=New TAssignStmt.Create( "=",New TVarExpr.Create( indexVar ),New TBinaryMathExpr.Create( "+",New TVarExpr.Create( indexVar ),New TCastExpr.Create( varty,stp,1 ) ) )
- Else
- ' varty is NULL here for the casts. We will back-populate it later.
- ' init=New TAssignStmt.Create( "=",New TIdentExpr.Create( varid ),from )
- ' expr=New TBinaryCompareExpr.Create( op,New TIdentExpr.Create( varid ),New TCastExpr.Create( varty,term,1 ) )
- ' incr=New TAssignStmt.Create( "=",New TIdentExpr.Create( varid ),New TBinaryMathExpr.Create( "+",New TIdentExpr.Create( varid ),New TCastExpr.Create( varty,stp,1 ) ) )
- init=New TAssignStmt.Create( "=",varExpr,from )
- expr=New TBinaryCompareExpr.Create( op,varExpr,New TCastExpr.Create( varty,term,1 ) )
- incr=New TAssignStmt.Create( "=",varExpr,New TBinaryMathExpr.Create( "+",varExpr,New TCastExpr.Create( varty,stp,1 ) ) )
- EndIf
- Local block:TBlockDecl=New TBlockDecl.Create( _block )
- PushBlock block
- While Not CParse( "next" )
- 'If CParse( "end" )
- ' CParse "for"
- ' Exit
- 'EndIf
- ParseStmt
- Wend
- PopBlock
- NextToke
- Local stmt:TForStmt=New TForStmt.Create( init,expr,incr,block,loopLabel )
- _block.AddStmt stmt
- End Method
- Method ParseDefDataStmt(label:TLoopLabelDecl = Null)
- Parse "defdata"
-
- If AtEos() Then
- Err "Expecting expression but encountered " + DescribeToke(_toke)
- End If
-
- Local args:TExpr[]
- Local nargs:Int
- Repeat
- Local arg:TExpr
- If _toke And _toke<>"," arg=ParseExpr()
- If args.Length=nargs args=args + New TExpr[10]
- args[nargs]=arg
- nargs:+1
- Until Not CParse(",")
- args=args[..nargs]
-
- Local dataLabel:TDataLabelDecl
- If label Then
- dataLabel = New TDataLabelDecl.Create(label.ident, label.attrs)
- End If
-
- Local decl:TDefDataDecl = New TDefDataDecl.Create(args, dataLabel)
-
- _app.dataDefs.AddLast(decl)
-
- End Method
- Method ParseReadDataStmt()
- Parse "readdata"
- Local args:TExpr[]
- Local nargs:Int
- If Not AtEos() Then
- Repeat
- Local arg:TExpr
- If _toke And _toke<>"," arg=ParseExpr()
- If args.Length=nargs args=args + New TExpr[10]
- args[nargs]=arg
- nargs:+1
- Until Not CParse(",")
- args=args[..nargs]
- End If
- _block.AddStmt New TReadDataStmt.Create( args )
- End Method
- Method ParseRestoreDataStmt()
- Parse "restoredata"
-
- Local expr:TExpr = ParseExpr()
- _block.AddStmt New TRestoreDataStmt.Create( expr )
- End Method
-
- Method ParseReturnStmt()
- Parse "return"
- Local expr:TExpr
- If Not AtEos() expr=ParseExpr()
- _block.AddStmt New TReturnStmt.Create( expr )
- End Method
- Method ParseExitStmt()
- Parse "exit"
- Local expr:TExpr
- If Not AtEos() expr=ParseExpr()
- _block.AddStmt New TBreakStmt.Create(expr)
- End Method
- Method ParseContinueStmt()
- Parse "continue"
- Local expr:TExpr
- If Not AtEos() expr=ParseExpr()
- _block.AddStmt New TContinueStmt.Create(expr)
- End Method
- Method ParseTryStmt()
- Parse "try"
- Local block:TBlockDecl=New TBlockDecl.Create( _block )
- Local catches:TList=New TList
- PushBlock block
- While _toke<>"end" And _toke<>"endtry"
- If CParse( "catch" )
- Local id:String=ParseIdent()
- Local ty:TType
- If Not CParse(":") Then
- Parse "$"
- ty= TType.stringType
- Else
- ty=ParseType()
- End If
- Local init:TLocalDecl=New TLocalDecl.Create( id,ty,Null,0 )
- Local block:TBlockDecl=New TBlockDecl.Create( _block )
- catches.AddLast(New TCatchStmt.Create( init,block ))
- PopBlock
- PushBlock block
- Else
- ParseStmt
- If _toke = "end" Then
- NextToke
- If _toke = "try" Then
- ' we are done with the try statement
- Exit
- Else
- ParseEndStmt(False)
- End If
- End If
- End If
- Wend
- PopBlock
-
- If Not CParse("endtry") Then
- ' TODO : handle case of no catch - perhaps throw the exception again.
- 'If Not catches.Length() Err "Try block must have at least one catch block"
- NextToke
- CParse "try"
- End If
- _block.AddStmt New TTryStmt.Create( block,TCatchStmt[](catches.ToArray()) )
- End Method
- Method ParseThrowStmt()
- Parse "throw"
- Local expr:TExpr = ParseExpr()
- _block.AddStmt New TThrowStmt.Create( expr )
- End Method
- Method ParseReleaseStmt()
- Parse "release"
- Local expr:TExpr = ParseExpr()
- _block.AddStmt New TReleaseStmt.Create( expr )
- End Method
-
- Method ParseAssertStmt()
- Parse "assert"
- Local expr:TExpr = ParseExpr()
- Local elseExpr:TExpr
- If _toke = "," Or _toke = "else" Then
- NextToke
- elseExpr = ParseExpr()
- End If
- _block.AddStmt New TAssertStmt.Create( expr, elseExpr )
- End Method
- Method ParseEndStmt(eatEnd:Int = True)
- If eatEnd Then
- Parse "end"
- End If
- _block.AddStmt New TEndStmt.Create( )
- End Method
- Method ParseSelectStmt()
- Parse "select"
- Local block:TBlockDecl=_block
- Local tmpVar:TLocalDecl=New TLocalDecl.Create( "",Null,ParseExpr(),,True )
- block.AddStmt New TDeclStmt.Create( tmpVar )
- While _toke<>"end" And _toke<>"default" And _toke<>"endselect"
- SetErr
- Select _toke
- Case "~n"
- NextToke
- Case "case"
- NextToke
- Local comp:TExpr
- Repeat
- Local expr:TExpr=New TVarExpr.Create( tmpVar )
- expr=New TBinaryCompareExpr.Create( "=",expr,ParseExpr() )
- If comp
- comp=New TBinaryLogicExpr.Create( "or",comp,expr )
- Else
- comp=expr
- EndIf
- Until Not CParse(",")
- Local thenBlock:TBlockDecl=New TBlockDecl.Create( _block )
- Local elseBlock:TBlockDecl=New TBlockDecl.Create( _block )
- Local ifstmt:TIfStmt=New TIfStmt.Create( comp,thenBlock,elseBlock )
- block.AddStmt ifstmt
- block=ifstmt.thenBlock
- PushBlock block
- Local fin:Int = False
- While _toke<>"case" And _toke<>"default" And _toke<>"end" And _toke<>"endselect"
- ParseStmt
- If _toke = "end" Then
- NextToke
- If _toke = "select" Then
- ' we are done with the select statement, full exit
- fin = True
- Exit
- Else
- ParseEndStmt(False)
- End If
- End If
- Wend
- PopBlock
- block=elseBlock
- If fin Exit
- Default
- Err "Syntax error - expecting 'Case', 'Default' or 'End'."
- End Select
- Wend
- If _toke="default"
- NextToke
- PushBlock block
- While _toke<>"end" And _toke<>"endselect"
- SetErr
- Select _toke
- Case "case"
- Err "Case can not appear after default."
- Case "default"
- Err "Select statement can have only one default block."
- End Select
- ParseStmt
- If _toke = "end" Then
- NextToke
- If _toke = "select" Then
- Exit
- Else
- ParseEndStmt(False)
- End If
- End If
- Wend
- PopBlock
- EndIf
- SetErr
- If Not CParse("endselect") Then
- If Not CParse("select")
- Parse "end"
- Parse "select"
- End If
- End If
- End Method
- Method ParseExternBlock(mdecl:TModuleDecl, attrs:Int)
- NextToke
- If _tokeType=TOKE_STRINGLIT
- Local api:String = ParseStringLit().ToLower()
- If api = "win32" Then
- attrs:| DECL_API_WIN32
- End If
- End If
- attrs = attrs | DECL_EXTERN
- If CParse( "private" ) attrs=attrs|DECL_PRIVATE
- While _toke<>"endextern"
- If CParse( "end" )
- Parse "extern"
- Exit
- EndIf
- SetErr
- Select _toke
- Case "~n"
- NextToke
- Case "const","global"
- mdecl.InsertDecls ParseDecls( _toke,attrs )
- Case "struct"
- mdecl.InsertDecl ParseClassDecl( _toke,attrs | CLASS_STRUCT )
- Case "type"
- mdecl.InsertDecl ParseClassDecl( _toke,attrs )
- Case "function"
- mdecl.InsertDecl ParseFuncDecl( _toke,attrs )
- Case "interface"
- mdecl.InsertDecl ParseClassDecl( _toke,attrs | CLASS_INTERFACE )
- Default
- If _toke <> "end" And _toke <> "endextern" Then
- Err "Expecting expression but encountered '"+_toke+"'"
- End If
- End Select
- Wend
-
- If _toke="endextern" Then
- NextToke
- End If
- End Method
- Method ParseStmt()
- SetErr
- Select _toke
- Case ";","~n"
- NextToke
- Case "const","local","global"
- ParseDeclStmts
- ' nested function - needs to get added to the "module"
- Case "function"
- _block.InsertDecl ParseFuncDecl( _toke,FUNC_NESTED)
- Case "return"
- ParseReturnStmt()
- Case "exit"
- ParseExitStmt()
- Case "continue"
- ParseContinueStmt()
- Case "if"
- ParseIfStmt( "" )
- Case "while"
- ParseWhileStmt()
- Case "repeat"
- ParseRepeatStmt()
- Case "for"
- ParseForStmt()
- Case "select"
- ParseSelectStmt()
- Case "assert"
- ParseAssertStmt()
- Case "try"
- ParseTryStmt()
- Case "throw"
- ParseThrowStmt()
- Case "end"
- ParseEndStmt()
- Case "extern"
- ParseExternBlock(_module, 0)
- Case "#"
- Local decl:TLoopLabelDecl = ParseLoopLabelDecl()
- NextToke
- While _toke
- SetErr
- Select _toke.ToLower()
- Case "~n"
- NextToke
- Case "while"
- ParseWhileStmt(decl)
- Exit
- Case "repeat"
- ParseRepeatStmt(decl)
- Exit
- Case "for"
- ParseForStmt(decl)
- Exit
- Case "defdata"
- ParseDefDataStmt(decl)
- Exit
- Default
- Err "Labels must appear before a loop or DefData statement"
- End Select
- Wend
- Case "release"
- ParseReleaseStmt()
- Case "defdata"
- ParseDefDataStmt()
- Case "readdata"
- ParseReadDataStmt()
- Case "restoredata"
- ParseRestoreDataStmt()
- Default
- If _toke.StartsWith("'!") Then
- If _tokeType = TOKE_NATIVE Then
- ParseNativeStmt()
- End If
- Else
- Local expr:TExpr=ParsePrimaryExpr( True )
-
- Select _toke.ToLower()
- '"=","*=","/=","+=","-=","&=","|=","~~=","Mod","Shl","Shr"
- Case "=",":*",":/",":+",":-",":&",":|",":~~","mod","shl","shr", ":shl", ":shr", "sar", ":sar", ":mod"
- ' remap symbols...
- 'For Local i:Int = 0 Until TToker._symbols.length
- ' Local sym$= TToker._symbols[i]
- ' If _toke.ToLower() = sym
- ' _toke = TToker._symbols_map[i]
- ' Exit
- ' EndIf
- 'Next
-
-
- If TIdentExpr( expr ) Or TIndexExpr( expr )
- Local op$=_toke.ToLower()
- NextToke
- ' If Not op.EndsWith( "=" ) And Not op.StartsWith("=")
- ' Parse "="
- ' op:+"="
- ' EndIf
- _block.AddStmt New TAssignStmt.Create( op,expr,ParseExpr() )
- Else
- Err "Assignment operator '"+_toke+"' cannot be used this way."
- EndIf
- Return
- End Select
-
- If TIdentExpr( expr )
-
- expr=New TFuncCallExpr.Create( expr,ParseArgs( True ) )
-
- Else If TFuncCallExpr( expr) Or TInvokeSuperExpr( expr ) Or TNewObjectExpr( expr ) Or TNewExpr(expr)
-
- Else If TIndexExpr(expr)
- expr = New TFuncCallExpr.Create( expr, ParseArgs( True ) )
- Else
- Err "Expression cannot be used as a statement."
- EndIf
-
- _block.AddStmt New TExprStmt.Create( expr )
- End If
- End Select
- End Method
- Method ParseDecl:TDecl( toke$,attrs:Int )
- SetErr
- Local id$=ParseIdent()
- Local ty:TType
- Local init:TExpr
- If attrs & DECL_EXTERN
- ty=ParseDeclType()
- ' If CParse("(") Then
- If _toke = "(" Then
- ' function pointer?
- Local decl:TFuncDecl = ParseFuncDecl("", attrs | FUNC_PTR)
- If Not ty Then
- ty = New TFunctionPtrType
- TFunctionPtrType(ty).func = decl
- Else
- decl.retType = ty
- ty = New TFunctionPtrType
- TFunctionPtrType(ty).func = decl
- End If
- TFunctionPtrType(ty).func.ident = id
- Else If toke = "const" Then
- If CParse("=") Then
- init=ParseExpr()
- End If
- End If
- Else If CParse( ":=" )
- init=ParseExpr()
- Else
- ty=ParseDeclType()
- If CParse( "=" )
- init=ParseExpr()
- Else If CParse( "[" )
- Local ln:TExpr[]
- Repeat
- If CParse(",") Then
- ln = ln + [New TNullExpr]
- Continue
- End If
- If CParse("]") Exit
- ln = ln + [ParseExpr()]
- If CParse("]") Exit
- Parse(",")
- Forever
- 'Parse "]"
- ty = ParseArrayType(ty)
- 'While CParse( "[]" )
- ' ty=New TArrayType.Create(ty)
- 'Wend
- init=New TNewArrayExpr.Create( ty,ln)
- ty=New TArrayType.Create( ty, ln.length )
- Else If _toke = "(" Then
- ' function pointer?
- Local fdecl:TFuncDecl = ParseFuncDecl("", FUNC_PTR)
- If toke = "field" Then
- fdecl.attrs :| FUNC_METHOD
- End If
- If Not ty Then
- ty = New TFunctionPtrType
- TFunctionPtrType(ty).func = fdecl
- Else
- fdecl.retType = ty
- ty = New TFunctionPtrType
- TFunctionPtrType(ty).func = fdecl
- End If
- TFunctionPtrType(ty).func.ident = ""
- ' an initialised array of function pointers?
- If Not IsArrayDef() And CParse( "[" )
- Local ln:TExpr[]
- Repeat
- If CParse(",") Then
- ln = ln + [New TNullExpr]
- Continue
- End If
- If CParse("]") Exit
- ln = ln + [ParseExpr()]
- If CParse("]") Exit
- Parse(",")
- Forever
- 'Parse "]"
- ty = ParseArrayType(ty)
- 'While CParse( "[]" )
- ' ty=New TArrayType.Create(ty)
- 'Wend
- init=New TNewArrayExpr.Create( ty,ln)
- ty=New TArrayType.Create( ty, ln.length )
- Else
- While IsArrayDef()
- ty = ParseArrayType(ty)
- Wend
- ' check for function pointer init
- If CParse("=") Then
- init=ParseExpr()
- Else
- init=New TConstExpr.Create( ty,"" )
- End If
- End If
- Else If toke<>"const"
- init=New TConstExpr.Create( ty,"" )
- Else
- Err "Constants must be initialized."
- EndIf
- EndIf
- Local decl:TValDecl
- Select toke
- Case "global" decl=New TGlobalDecl.Create( id,ty,init,attrs )
- Case "field" decl=New TFieldDecl.Create( id,ty,init,attrs )
- Case "const" decl=New TConstDecl.Create( id,ty,init,attrs )
- Case "local" decl=New TLocalDecl.Create( id,ty,init,attrs )
- End Select
- If decl.IsExtern()
- 'DebugStop
- If CParse( "=" )
- decl.munged=ParseStringLit()
- Else
- decl.munged=decl.ident
- EndIf
- If TFunctionPtrType(ty) Then
- TFunctionPtrType(ty).func.munged = decl.munged
-
- Local cdets:TCastDets = TCastDets(_externCasts.ValueForKey(TFunctionPtrType(ty).func.munged))
- If cdets Then
- TFunctionPtrType(ty).func.castTo = cdets.retType
- If cdets.noGen Then
- TFunctionPtrType(ty).func.noCastGen = True
- End If
- For Local i:Int = 0 Until cdets.args.length
- If i < TFunctionPtrType(ty).func.argDecls.length Then
- TFunctionPtrType(ty).func.argDecls[i].castTo = cdets.args[i]
- End If
- Next
- End If
-
- End If
- EndIf
- 'meta data for variables
- If CParse( "{" ) Then
- 'print "meta for variable: "+id+ " -> "+ParseMetaData()
- decl.metadata = ParseMetaData()
- EndIf
- Return decl
- End Method
- Method ParseDecls:TList( toke$,attrs:Int )
- If toke Parse toke
- Local decls:TList=New TList'<Decl>
- Repeat
- Local decl:TDecl=ParseDecl( toke,attrs )
- decls.AddLast decl
- If Not CParse(",") Return decls
- Forever
- End Method
- Method ParseDeclStmts()
- Local toke$=_toke
- NextToke
- Repeat
- Local decl:TDecl=ParseDecl( toke,0 )
- _block.AddStmt New TDeclStmt.Create( decl )
-
- ' reset the decl scope, adding decl to the block decl list.
- ' this improves scope visibilty - for decls such as embedded functions
- If TConstDecl(decl) Or TGlobalDecl(decl) Then
- decl.scope = Null
- _block.InsertDecl(decl)
- If TGlobalDecl(decl) Then
- TGlobalDecl(decl).funcGlobal = True
- End If
- End If
-
- Until Not CParse(",")
- End Method
-
- Method ParseLoopLabelDecl:TLoopLabelDecl()
- NextToke
- Local id:String = ParseIdent()
- Return New TLoopLabelDecl.Create(id, 0)
- End Method
- 'handle end-of-line "dot dot return"-line connector
- '-> skips EOL tokens
- Method HandleDotsLineConnector(eatToke:Int = False)
- Local tok:TToker = New TToker.Copy(_toker)
- Local t:String = tok.NextToke()
- Local count:Int = tok.SkipSpace()
- For Local i:Int = 0 Until count
- NextToke
- Next
-
- t = tok._toke
- If t = "~r" Then
- t = tok.NextToke()
- If t = "~n" Then
- NextToke
- NextToke
- Else
- NextToke
- End If
- Else
- If t = "~n" Then
- NextToke
- End If
- End If
-
- If eatToke And Not count Then
- NextToke
- End If
- End Method
- 'should return a specific "metadata object" ?
- ' metadata is in the form : {key key=value key="value"}
- Method ParseMetaData:TMetadata()
- Local meta:TMetadata = New TMetadata
- SkipEols
- Repeat
-
- If meta.metadataString Then
- meta.metadataString :+ " "
- End If
-
- Select _tokeType
- Case TOKE_INTLIT
- Err "Expecting '}' but encountered integer literal"
- Case TOKE_FLOATLIT
- Err "Expecting '}' but encountered floating point literal"
- Case TOKE_STRINGLIT
- Err "Expecting '}' but encountered string literal"
- Case TOKE_SYMBOL
- Err "Expecting '}' but encountered " + _toke
- End Select
-
- 'append current token to metaDataString
- Local key:String = _toke
- meta.metadataString :+ key
- 'read next token
- NextToke()
- Local value:String
- ' got a value
- If CParse("=") Then
-
- If _tokeType = TOKE_IDENT Then
- Err "Meta data must be literal constant"
- End If
-
- value = _toke
- meta.metadataString :+ "=" + value
- 'read next token
- NextToke()
- Else
- value = "1"
- meta.metadataString :+ "=1"
- End If
-
- meta.InsertMeta(key.ToLower(), value)
-
- 'reached end of meta data declaration
- If _toke="}" Then Exit
- Forever
- 'continue to next token
- NextToke()
- 'parse this into something
- Return meta
- End Method
- Method ParseFuncDecl:TFuncDecl( toke$,attrs:Int, returnType:TType = Null )
- SetErr
- If toke Parse toke
- Local id$
- Local ty:TType
- Local meth:Int = attrs & FUNC_METHOD
- Local meta:TMetadata
- Local noMangle:Int
- If Not returnType Then
- If attrs & FUNC_METHOD
- If _toke="new"
- If attrs & DECL_EXTERN
- Err "Extern classes cannot have constructors"
- EndIf
- id="New"
- NextToke
- attrs:|FUNC_CTOR
- attrs:&~FUNC_METHOD
- Else If _toke="operator" Then
- attrs:|FUNC_OPERATOR
- NextToke
-
- Local t:String = _toke.ToLower()
- NextToke
-
- Select t
- Case "*","/","+","-","&","|","~~"
- id = t
- Case ":*",":/",":+",":-",":&",":|",":~~"
- id = t
- Case "<",">"',"="',"<=",">=","=","<>"
- If CParse("=") Then
- t :+ "="
- Else If t = "<" And CParse(">") Then
- t :+ ">"
- End If
- id = t
- Case "="
- id = t
- Case "mod", "shl", "shr"
- id = t
- Case ":mod", ":shl", ":shr"
- id = t
- Default
- DoErr "Operator must be one of: * / + - & | ~~ :* :/ :+ :- :& :| :~~ < > <= >= = <> mod shl shr :mod :shl :shr"
- End Select
- ty=ParseDeclType()
- Else
- id=ParseIdent()
- ty=ParseDeclType()
- If ty._flags & (TType.T_CHAR_PTR | TType.T_SHORT_PTR) Then
- DoErr "Illegal function return type"
- End If
-
- ' Delete() return type should always be Void
- If id.ToLower() = "delete" Then
- attrs:|FUNC_DTOR
- If TIntType(ty) Then
- ty = New TVoidType
- End If
- End If
- EndIf
- Else
- If Not (attrs & FUNC_PTR) Then
- id=ParseIdent()
- ty=ParseDeclType()
- ' can only return "$z" and "$w" from an extern function.
- If ty._flags & (TType.T_CHAR_PTR | TType.T_SHORT_PTR) And Not (attrs & DECL_EXTERN) Then
- DoErr "Illegal function return type"
- End If
- End If
- EndIf
- End If
-
- Local args:TArgDecl[]
- Parse "("
- SkipEols
- If _toke<>")"
- Local nargs:Int
- Repeat
- Local argId$=ParseIdent()
- Local ty:TType=ParseDeclType()
- Local init:TExpr
- ' function pointer ?
- If _toke = "(" Then
- Local fdecl:TFuncDecl = ParseFuncDecl("", FUNC_PTR | DECL_ARG)
- If Not ty Then
- ty = New TFunctionPtrType
- TFunctionPtrType(ty).func = fdecl
- Else
- fdecl.retType = ty
- ty = New TFunctionPtrType
- TFunctionPtrType(ty).func = fdecl
- End If
- TFunctionPtrType(ty).func.ident = argId
- ' function pointer array ?
- While IsArrayDef()
- ty = ParseArrayType(ty)
- Wend
- End If
-
- ' var argument?
- If CParse("var") Then
- ty = TType.MapToVarType(ty)
- Else If CParse( "=" )
- init=ParseExpr()
- End If
-
- Local arg:TArgDecl=New TArgDecl.Create( argId,ty,init )
- If args.Length=nargs args=args + New TArgDecl[10]
- args[nargs]=arg
- nargs:+1
- If _toke=")" Exit
- Parse ","
- Forever
- args=args[..nargs]
- EndIf
- Parse ")"
-
- If returnType Then
- Return New TFuncDecl.CreateF(Null,returnType,args,attrs)
- End If
-
- Local fdecl:TFuncDecl
- ' wait.. so everything until now was a function pointer return type, and we still have to process the function declaration...
- If _toke = "(" Then
- Local retTy:TType = New TFunctionPtrType
- TFunctionPtrType(retTy).func = New TFuncDecl.CreateF("",ty,args,attrs )
- TFunctionPtrType(retTy).func.attrs :| FUNC_PTR
- fdecl = ParseFuncDecl("", attrs, retTy)
- ty = retTy
- End If
- Repeat
- If CParse( "final" )
- attrs:|DECL_FINAL
- Else If CParse( "abstract" )
- attrs:|DECL_ABSTRACT
- Else If CParse( "property" )
- If attrs & FUNC_METHOD
- attrs:|FUNC_PROPERTY
- Else
- Err "Only methods can be properties."
- EndIf
- Else If CParse( "nodebug" )
- ' TODO : NoDebug
- attrs :| DECL_NODEBUG
- Else If CParse( "{" ) 'meta data
- ' TODO : do something with the metadata
- 'meta data for functions/methods
- 'print "meta for func/meth: "+id+ " -> "+ParseMetaData()
- meta = ParseMetaData()
-
- If meta.HasMeta("nomangle") Then
- If attrs & FUNC_METHOD Then
- Err "Only functions can specify NoMangle"
- Else
- noMangle = True
- End If
- End If
- Else If _tokeType=TOKE_STRINGLIT
- ' "win32", etc
- ' TODO ? something with this??
- Local api:String = ParseStringLit().ToLower()
- If api = "win32" Then
- attrs :| DECL_API_WIN32
- End If
- Else
- Exit
- EndIf
- Forever
- Local funcDecl:TFuncDecl
- If attrs & FUNC_CTOR Then
- funcDecl=New TNewDecl.CreateF( id,ty,args,attrs )
- Else
- If fdecl Then
- funcDecl = fdecl
- funcDecl.ident = id
- Else
- funcDecl=New TFuncDecl.CreateF( id,ty,args,attrs )
- End If
- funcDecl.noMangle = noMangle
- End If
- If meta Then
- funcDecl.metadata = meta
- End If
- If funcDecl.IsExtern() Or (attrs & FUNC_PTR)
- funcDecl.munged=funcDecl.ident
- ' a normal function pointer definition *probably* can't be defined with a munged name?
- ' If there is an equals here, one can assume it is for an initialisation...
- 'If (Not (attrs & FUNC_PTR)) Or (attrs & FUNC_PTR And Not (attrs & DECL_ARG)) Then
- If Not (attrs & FUNC_PTR) Then
- If CParse( "=" )
- funcDecl.munged=ParseStringLit()
- End If
- 'Array $resize hack!
- 'If funcDecl.munged="$resize"
- ' funcDecl.retTypeExpr=TType.emptyArrayType
- 'EndIf
- EndIf
- If funcDecl.munged Then
- ' look up extern cast list
- Local cdets:TCastDets = TCastDets(_externCasts.ValueForKey(funcDecl.munged))
- If cdets Then
- funcDecl.castTo = cdets.retType
- If cdets.noGen Then
- funcDecl.noCastGen = True
- End If
- For Local i:Int = 0 Until cdets.args.length
- If i < funcDecl.argDecls.length Then
- funcDecl.argDecls[i].castTo = cdets.args[i]
- End If
- Next
- End If
- End If
- Return funcDecl
- EndIf
- If funcDecl.IsAbstract() Return funcDecl
- 'Ok, only first statement of a constructor can call super constructor - not pretty, should be in semant.
- If attrs & FUNC_CTOR
- SkipEols
- If CParse( "super" )
- Parse "."
- If _toke="new"
- Local id$="New"
- NextToke
- 'funcDecl.superCtor=New TInvokeSuperExpr.Create( id,ParseArgs( True ) )
- 'funcDecl.AddStmt New TExprStmt.Create( funcDecl.superCtor )
- funcDecl.AddStmt New TExprStmt.Create( New TNewExpr.Create(ParseArgs(True), True))
- Else
- Local id$=ParseIdent()
- funcDecl.AddStmt New TExprStmt.Create( New TInvokeSuperExpr.Create( id,ParseArgs( True ) ) )
- EndIf
- Else
- 'Invoke super default ctor
- 'funcDecl.superCtor=New InvokeSuperExpr( "new",[] )
- 'funcDecl.AddStmt New TExprStmt( funcDecl.superCtor )
- EndIf
- EndIf
- PushBlock funcDecl
- While (Not meth And _toke.ToLower()<>"endfunction") Or (meth And _toke.ToLower()<>"endmethod")
- If CParse( "end" )
- If (Not meth And CParse("function")) Or (meth And CParse("method"))
- Exit
- End If
- ' handle "end" statement
- ParseEndStmt(False)
- EndIf
- ParseStmt
- Wend
- PopBlock
- NextToke
- 'If toke CParse toke
- Return funcDecl
- End Method
- Method ParseClassDecl:TClassDecl( toke$,attrs:Int )
- SetErr
- If toke Parse toke
- Local id$=ParseIdent()
- Local args:TStack = New TStack
- Local superTy:TIdentType
- Local imps:TIdentType[]
- Local meta:TMetadata
- 'If (attrs & CLASS_INTERFACE) And (attrs & DECL_EXTERN)
- ' Err "Interfaces cannot be extern."
- 'EndIf
- If CParse( "<" )
- If attrs & DECL_EXTERN
- Err "Extern classes cannot be generic."
- EndIf
- 'If attrs & CLASS_INTERFACE
- ' Err "Interfaces cannot be generic."
- 'EndIf
- 'If attrs & CLASS_TEMPLATEARG
- ' Err "Class parameters cannot be generic."
- 'EndIf
- Local nargs:Int
- Repeat
- 'Local decl:TClassDecl=ParseClassDecl( "",CLASS_TEMPLATEARG )
- 'If args.Length=nargs args=args + New TClassDecl[10]
- 'args[nargs]=decl
- 'nargs:+1
- args.Push ParseIdent()
- Until Not CParse(",")
- 'args=args[..nargs]
- Parse ">"
- EndIf
- If CParse( "extends" )
- 'If attrs & CLASS_TEMPLATEARG
- ' Err "Extends cannot be used with class parameters."
- 'EndIf
- ' If CParse( "null" )
- '
- If attrs & CLASS_STRUCT
- Err "Structs cannot be extended"
- EndIf
- '
- ' If Not (attrs & DECL_EXTERN)
- ' Err "Only extern objects can extend null."
- ' EndIf
- '
- ' superTy=Null
- '
- ' Else
- If attrs & CLASS_INTERFACE And Not (attrs & DECL_EXTERN)
- Local nimps:Int
- Repeat
- If imps.Length=nimps imps=imps + New TIdentType[10]
- imps[nimps]=ParseIdentType()
- nimps:+1
- Until Not CParse(",")
- imps=imps[..nimps]
- superTy=New TIdentType.Create( "brl.classes.object" )
- Else
- superTy=ParseIdentType()
- EndIf
- Else
- If Not (attrs & DECL_EXTERN) Then
- superTy=New TIdentType.Create( "brl.classes.object" )
- End If
- EndIf
- If CParse( "implements" )
- If attrs & CLASS_STRUCT
- Err "Implements cannot be used with Structs"
- EndIf
- 'If attrs & DECL_EXTERN
- ' Err "Implements cannot be used with external classes."
- 'EndIf
- If attrs & CLASS_INTERFACE
- Err "Implements cannot be used with interfaces."
- EndIf
- 'If attrs & CLASS_TEMPLATEARG
- ' Err "Implements cannot be used with class parameters."
- 'EndIf
- Local nimps:Int
- Repeat
- If imps.Length=nimps imps=imps + New TIdentType[10]
- imps[nimps]=ParseIdentType()
- nimps:+1
- Until Not CParse(",")
- imps=imps[..nimps]
- EndIf
- Repeat
- If CParse( "final" )
- If attrs & CLASS_INTERFACE
- Err "Final cannot be used with interfaces."
- End If
-
- If attrs & CLASS_STRUCT
- Err "Final cannot be used with structs."
- End If
-
- If attrs & DECL_FINAL
- Err "Duplicate type attribute."
- End If
- If attrs & DECL_ABSTRACT
- Err "Classes cannot be both final and abstract."
- End If
-
- attrs:|DECL_FINAL
- Else If CParse( "abstract" )
- If attrs & CLASS_INTERFACE
- Err "Abstract cannot be used with interfaces."
- EndIf
-
- If attrs & CLASS_STRUCT
- Err "Abstract cannot be used with structs."
- EndIf
-
- If attrs & DECL_ABSTRACT
- Err "Duplicate type attribute."
- End If
-
- If attrs & DECL_FINAL
- Err "Types cannot be both final and abstract."
- End If
- attrs:|DECL_ABSTRACT
- Else
- Exit
- EndIf
- Forever
- 'check for metadata
- If CParse( "{" )
- If attrs & CLASS_STRUCT
- Err "Structs cannot store metadata."
- EndIf
- meta = ParseMetaData()
- End If
- Local classDecl:TClassDecl=New TClassDecl.Create( id,String[](args.ToArray()),superTy,imps,attrs )
-
- If meta Then
- classDecl.metadata = meta
- End If
- If classDecl.IsExtern()
- classDecl.munged=classDecl.ident
- If CParse( "=" ) classDecl.munged=ParseStringLit()
- EndIf
- 'If classDecl.IsTemplateArg() Return classDecl
- Local decl_attrs:Int=(attrs & DECL_EXTERN) | (attrs & DECL_NODEBUG) | (attrs & DECL_API_WIN32)
- Local method_attrs:Int=decl_attrs|FUNC_METHOD | (attrs & DECL_NODEBUG)
- If attrs & CLASS_INTERFACE method_attrs:|DECL_ABSTRACT
-
- Repeat
- SkipEols
- Select _toke
- Case "end"
- NextToke
- Exit
- Case "endtype"
- If attrs & CLASS_INTERFACE Then
- Err "Syntax error - expecting End Interface, not 'EndType'"
- End If
- If attrs & CLASS_STRUCT Then
- Err "Syntax error - expecting End Struct, not 'EndType'"
- End If
- toke = Null
- NextToke
- Exit
- Case "endstruct"
- If attrs & CLASS_INTERFACE Then
- Err "Syntax error - expecting End Interface, not 'EndStruct'"
- End If
- If Not (attrs & CLASS_STRUCT) Then
- Err "Syntax error - expecting End Type, not 'EndStruct'"
- End If
- toke = Null
- NextToke
- Exit
- Case "endinterface"
- If Not (attrs & CLASS_INTERFACE) And Not (attrs & CLASS_STRUCT) Then
- Err "Syntax error - expecting End Type, not 'EndInterface'"
- End If
- If Not (attrs & CLASS_INTERFACE) And (attrs & CLASS_STRUCT) Then
- Err "Syntax error - expecting End Struct, not 'EndInterface'"
- End If
- toke = Null
- NextToke
- Exit
- Case "private"
- NextToke
- decl_attrs=decl_attrs | DECL_PRIVATE
- Case "public"
- NextToke
- decl_attrs=decl_attrs & ~DECL_PRIVATE
- Case "const","global","field"
- If attrs & DECL_EXTERN Then
- If (attrs & CLASS_INTERFACE) Then
- Err "Extern Interfaces can only contain methods."
- End If
- If Not (attrs & CLASS_STRUCT) Then
- Err "Extern Types can only contain methods."
- End If
- End If
- If (attrs & CLASS_INTERFACE) And _toke<>"const"
- Err "Interfaces can only contain constants and methods."
- EndIf
- If (attrs & CLASS_STRUCT) And _toke<>"field"
- Err "Structs can only contain fields."
- EndIf
- classDecl.InsertDecls ParseDecls( _toke,decl_attrs )
- Case "method"
- If (attrs & CLASS_STRUCT) And (attrs & DECL_EXTERN) Then
- Err "Structs can only contain fields."
- EndIf
- Local decl:TFuncDecl=ParseFuncDecl( _toke,method_attrs )
- If decl.IsCtor() decl.retTypeExpr=New TObjectType.Create( classDecl )
- classDecl.InsertDecl decl
- Case "function"
- If (attrs & CLASS_INTERFACE)
- Err "Interfaces can only contain constants and methods."
- EndIf
- If attrs & CLASS_STRUCT Then
- If (attrs & DECL_EXTERN) Then
- Err "Structs can only contain fields."
- Else
- Err "Structs can only contain fields and methods."
- End If
- EndIf
- If attrs & DECL_EXTERN Then
- Err "Extern Types can only contain methods."
- End If
- Local decl:TFuncDecl=ParseFuncDecl( _toke,decl_attrs )
- classDecl.InsertDecl decl
- Default
- Err "Syntax error - expecting class member declaration, not '" + _toke + "'"
- End Select
- Forever
- If toke Parse toke
- Return classDecl
- End Method
-
- Method ParseNativeStmt()
- If Not _toke.StartsWith("'!") Then
- Err "Syntax error - expecting !'"
- End If
- Local raw:String = _toke[2..]
- _block.AddStmt New TNativeStmt.Create( raw )
- NextToke
- End Method
- Method ParseModuleDecl:String( toke$,attrs:Int )
- NextToke
- ' namespace . module
- Return ParseModPath().ToLower()
- End Method
- Method ParseModPath$()
- Local path$=ParseIdent()
- While CParse( "." )
- path:+"."+ParseIdent()
- Wend
- Return path
- End Method
- Method ExtractModIdent$( modpath$ )
- Local i:Int=modpath.FindLast( "." )
- If i<>-1 Return modpath[i+1..]
- Return modpath
- End Method
- Method ImportFile( filepath$ )
- If filepath.Endswith(".bmx") Then
- filepath = ActualPath(filepath)
- Local origPath:String = RealPath(filepath)
- Local path:String = OutputFilePath(origPath, FileMung(), "i")
- If FileType( path )<>FILETYPE_FILE
- Err "File '"+ path +"' not found."
- EndIf
- If _module.imported.Contains( path ) Return
- Local modpath:String
- If opt_buildtype = BUILDTYPE_MODULE Then
- Local dir:String = ExtractDir(origPath).ToLower()
- dir = dir[dir.findLast("/") + 1..]
- If dir.EndsWith(".mod") Then
- dir = ""
- Else
- dir :+ "_"
- End If
- Local file:String = StripDir(origPath).ToLower()
- modpath = opt_modulename + "_" + dir + StripExt(file)
- Else
- modpath = StripExt(filepath)
- End If
- 'sanitize the path, remove non-allowed chars
- modpath = TStringHelper.Sanitize(modpath.ToLower())
- ' try to import interface
- Local par:TIParser = New TIParser
- If par.ParseModuleImport(_module, modpath, origPath, path, , , filepath, True) Return
- Else If filepath.startswith("-") Then
- If Not _app.fileimports.Contains(filepath) Then
- _app.fileimports.AddLast filepath
- End If
- Else
- If filepath.EndsWith(".h") Or filepath.EndsWith(".hpp") Or filepath.EndsWith(".hxx") Then
- If filepath.Find("*") = -1 Then
- _app.headers.AddLast filepath
- End If
- Else
- Local path:String = ActualPath(RealPath(filepath))
- If FileType( path )<>FILETYPE_FILE
- Err "File '"+ path +"' not found."
- End If
- End If
- End If
- End Method
- Method ImportAllModules(attrs:Int)
- ' get all brl and pub modules
- Local mods:TList = EnumModules("brl")
- mods = EnumModules("pub", mods)
- For Local m:String = EachIn mods
- ImportModule(m, attrs)
- Next
- End Method
-
- Method ImportModule( modpath$,attrs:Int )
- SetErr
-
- modpath = modpath.ToLower()
- Local basepath:String = ModulePath(modpath.ToLower())
- If _module.imported.Contains( basepath ) Return
- ' try to import interface
- Local par:TIParser = New TIParser
- If par.ParseModuleImport(_module, modpath, basepath, , , attrs) Return
- 'DebugStop
- 'Local mdecl:TDecl=_app.imported.ValueForKey( basepath )
- 'If Not mdecl
- ' mdecl=ParseModule( filepath,_app )
- 'EndIf
- '_module.imported.Insert mdecl.filepath,mdecl
- 'If Not (attrs & DECL_PRIVATE) _module.pubImported.Insert mdecl.filepath,mdecl
- '_module.InsertDecl New AliasDecl( mdecl.ident,mdecl,attrs )
- 'End Rem
- End Method
- Method ValidateModIdent( id$ )
- If id.Length
- If IsAlpha( id[0] ) Or id[0]="_"[0]
- Local err:Int
- For Local i:Int=1 Until id.Length
- If IsAlpha( id[i] ) Or IsDigit( id[i] ) Or id[i]="_"[0] Continue
- err=1
- Exit
- Next
- If Not err Return
- EndIf
- EndIf
- Err "Invalid module identifier '"+id+"'."
- End Method
- Method MungAppDecl(app:TAppDecl)
- If opt_buildtype = BUILDTYPE_MODULE And opt_ismain Then
- app.munged = MungModuleName(opt_modulename)
- Else If opt_buildtype = BUILDTYPE_MODULE Then
- Local dir:String = ExtractDir(opt_filepath).ToLower()
- dir = dir[dir.findLast("/") + 1..]
- If dir.EndsWith(".mod") Then
- dir = ""
- Else
- dir :+ "_"
- End If
- app.munged = "_bb_" + opt_modulename + "_" + dir + StripExt(StripDir(opt_filepath).ToLower())
- Else
- ' main application file?
- If opt_apptype Then
- app.munged = "_bb_main"
- Else
- Local dir:String = ExtractDir(opt_filepath).ToLower()
- dir = dir[dir.findLast("/") + 1..]
- If dir.EndsWith(".mod") Then
- dir = dir.Replace(".mod", "")
- End If
- Local file:String = StripDir(opt_filepath).ToLower()
- app.munged = "_bb_" + dir + "_" + StripExt(file)
- End If
- End If
- 'sanitize, remove non-allowed chars
- app.munged = TStringHelper.Sanitize(app.munged)
- End Method
- ' load external cast defs
- Method LoadExternCasts(path:String)
- For Local externs:Int = 0 Until 3
-
- Local ePath:String
-
- ' we will iterate through all possibilities as there may be different sets
- ' of explicit casts/no gen funcs for each.
- Select externs
- Case 0
- ' eg. file.win32.x86.x
- ePath = StripExt(path) + "." + opt_platform + "." + opt_arch + ".x"
- Case 1
- ' eg. file.win32.x
- ePath = StripExt(path) + "." + opt_platform + ".x"
- Case 2
- ' eg. file.x
- ePath = StripExt(path) + ".x"
- End Select
- If FileType(ePath) = FILETYPE_FILE Then
-
- Local toker:TToker=New TToker.Create( ePath,LoadText( ePath ) )
- toker.NextToke
-
- While True
-
- SkipEolsToker(toker)
-
- If toker._tokeType = TOKE_EOF Exit
-
- Local rt$=toker._toke
- If CParseToker(toker, "unsigned") Then
- rt :+ " " + toker._toke
- End If
- NextTokeToker(toker)
-
- If CParseToker(toker,"*") Then
- rt:+ "*"
-
- If CParseToker(toker,"*") Then
- rt:+ "*"
- End If
- End If
-
-
- Local dets:TCastDets = New TCastDets
-
- If CParseToker(toker, "__stdcall") Then
- dets.api = "__stdcall"
- End If
- ' fname
- Local fn$=toker._toke
- NextTokeToker(toker)
-
- dets.name = fn
- dets.retType = rt
-
- _externCasts.Insert(fn, dets)
-
- ' args
- ParseToker(toker, "(")
-
- If CParseToker(toker, ")") Then
-
- ' don't generate header extern
- If CParseToker(toker, "!") Then
- dets.noGen = True
- End If
-
- Continue
- End If
-
- Local i:Int = 0
- Repeat
- Local at$=toker._toke
-
- If CParseToker(toker, "const") Then
- at :+ " " + toker._toke
- End If
-
- If CParseToker(toker, "unsigned") Then
- at :+ " " + toker._toke
- End If
- If CParseToker(toker, "struct") Then
- at :+ " " + toker._toke
- End If
-
- NextTokeToker(toker)
- If CParseToker(toker, "*") Then
- at:+ "*"
-
- If CParseToker(toker, "*") Then
- at:+ "*"
- End If
- End If
-
- ' function pointer
- If CParseToker(toker, "(") Then
-
- ParseToker(toker, "*")
- ParseToker(toker, ")")
- at :+ "(*)"
-
- ParseToker(toker, "(")
- at :+ "("
-
- While Not CParseToker(toker, ")")
- NextTokeToker(toker)
- at :+ toker._toke
- Wend
-
- at :+ ")"
- End If
-
-
- dets.args :+ [at]
-
- If toker._toke=")" Exit
- ParseToker(toker, ",")
-
- i:+ 1
- Forever
-
- NextTokeToker(toker)
-
- ' don't generate header extern
- If CParseToker(toker, "!") Then
- dets.noGen = True
- End If
-
- Wend
-
- End If
-
- Next
- End Method
- Method ParseCurrentFile:Int(path:String, attrs:Int)
- LoadExternCasts(path)
- While _toke
- SetErr
- Select _toke.toLower()
- Case "~n"
- NextToke
- Case "public"
- NextToke
- attrs=attrs & ~DECL_PRIVATE
- Case "private"
- NextToke
- attrs=attrs | DECL_PRIVATE
- Case "extern"
- ParseExternBlock(_module, attrs)
- Case "const"
- _module.InsertDecls ParseDecls( _toke,attrs )
- Case "global"
- Local list:TList = ParseDecls( _toke,attrs )
- _module.InsertDecls list
- For Local gdecl:TGlobalDecl = EachIn list
- gdecl.attrs :| DECL_INITONLY
- _block.AddStmt New TDeclStmt.Create( gdecl )
- Next
- Case "struct"
- _module.InsertDecl ParseClassDecl( _toke,attrs | CLASS_STRUCT )
- Case "type"
- _module.InsertDecl ParseClassDecl( _toke,attrs )
- Case "interface"
- _module.InsertDecl ParseClassDecl( _toke,attrs|CLASS_INTERFACE|DECL_ABSTRACT )
- Case "function"
- _module.InsertDecl ParseFuncDecl( _toke,attrs )
- Case "incbin"
- NextToke
- Local s:String = ParseStringLit()
- _app.mapStringConsts(s)
- Local ib:TIncBin = New TIncbin.Create(s, path)
- If Not ib Then
- DoErr "Incbin file '"+ s +"' not found."
- End If
- _app.incbins.AddLast(ib)
- Case "include"
- 'include command is NOT just a pattern to replace with
- 'content. BlitzMax parses each included file before the
- 'content gets appended to the source (right before
- 'semanting or analyzing content)
- NextToke
- Local includeFile:String = ParseStringLit()
- 'convert the URI of the to include file as it might be
- 'a relative one
- includeFile = RealPath(includeFile)
- 'instead of merging the data of multiple parsers, the
- 'same parser is used for all included files - but each
- 'of them uses an individual toker
- If FileType( includeFile )<>FILETYPE_FILE
- DoErr "File '"+ includeFile +"' not found."
- EndIf
- 'instead of "LoadText" "PreProcess" is used to include
- 'handling of conditionals and comments
- Try
- Local includeSource:String = PreProcess(includeFile)
- Local includeToker:TToker = New TToker.Create(includeFile, includeSource)
- 'backup old vars
- Local oldToker:TToker = Self._toker
- 'assign temporary vars
- Self._toker = includeToker
- 'parse the include file
- parseCurrentFile(includeFile, attrs)
- 'restore backup vars
- Self._toker = oldToker
- Catch e:TStreamException
- DoErr "Failed to include file '" + includeFile + "' : '" + e.ToString() + "'"
- End Try
- 'move on to next toke (after include "xyz.bmx")
- NextToke
- Default
- ParseStmt
- 'Err "Syntax error - expecting declaration."
- End Select
- Wend
- Return attrs
- End Method
- Method ParseMain()
- SkipEols
- Local mattrs:Int
- 'If CParse( "strict" ) mattrs:|MODULE_STRICT
- 'If CParse( "superstrict" ) mattrs:|MODULE_SUPERSTRICT
- Local path$=_toker.Path()
- Local ident$=StripAll( path )
- Local munged$ '="bb_"+ident+"_"
- If opt_buildtype = BUILDTYPE_MODULE And opt_ismain
- ValidateModIdent ident
- Else If opt_buildtype = BUILDTYPE_MODULE Then
- Local dir:String = ExtractDir(opt_filepath).ToLower()
- dir = dir[dir.findLast("/") + 1..]
- If dir.EndsWith(".mod") Then
- dir = ""
- Else
- dir :+ "_"
- End If
- munged = opt_modulename + "_" + dir + ident
- 'sanitize, remove non-allowed chars
- munged = TStringHelper.Sanitize(munged.ToLower())
- End If
- If opt_ismain Then 'And opt_modulename <> "brl.blitz" Then
- ident = opt_modulename
- End If
-
- If opt_buildtype = BUILDTYPE_APP Then
- ident = "m_" + ident
- End If
- _module=New TModuleDecl.Create( ident,munged,path,mattrs )
- _module.AddImport path,_module
- _app.InsertModule _module
- ' mung the app decl
- MungAppDecl(_app)
- If opt_buildtype = BUILDTYPE_MODULE And opt_modulename = "brl.blitz" Then
- ' import Object and String definitions
- Local par:TIParser = New TIParser
- par.ParseModuleImport(_module, "brl.classes", modulepath("brl.blitz"), modulepath("brl.blitz") + "/blitz_classes.i")
- ' set up built-in keywords
- par = New TIParser
- par.ParseModuleImport(_module, "brl.blitzkeywords", "", "", MakeKeywords())
- End If
- ' don't import ourself
- If opt_modulename <> "brl.blitz" Then
- Local par:TIParser = New TIParser
- par.ParseModuleImport(_module, "brl.blitz", modulepath("brl.blitz"), , , MODULE_ACTUALMOD)
- End If
- Local mainFunc:TFuncDecl = New TFuncDecl.CreateF("__LocalMain", New TIntType,Null,0)
- '_app.InsertDecl mainFunc
- _module.insertDecl(mainFunc)
- 'Local mainBlock:TBlockDecl = New TBlockDecl.Create( _block )
- ' import all brl and pub modules if we haven't specified one
- If opt_buildtype <> BUILDTYPE_MODULE And Not opt_framework Then
- ImportAllModules MODULE_ACTUALMOD
- Else
- If opt_framework Then
- Local par:TIParser = New TIParser
- par.ParseModuleImport(_module, opt_framework, modulepath(opt_framework), , , MODULE_ACTUALMOD)
- End If
- End If
- Local attrs:Int
- 'Parse header - imports etc.
- While _toke
- SetErr
- Select _toke.ToLower()
- Case "~n"
- NextToke
- Case "public"
- NextToke
- attrs=attrs & ~DECL_PRIVATE
- Case "private"
- NextToke
- attrs=attrs | DECL_PRIVATE
- Case "import"
- NextToke
- If _tokeType=TOKE_STRINGLIT
- ' TODO
- 'ImportFile ReplaceEnvTags( ParseStringLit() )
- ImportFile ParseStringLit()
- Else
- ImportModule ParseModPath(),attrs | MODULE_ACTUALMOD
- EndIf
- Case "framework"
- NextToke
- ImportModule ParseModPath(),attrs
- Case "alias"
- NextToke
- Repeat
- Local ident$=ParseIdent()
- Parse "="
- Local decl:Object
- Local scope:TScopeDecl=_module
- _env=_module 'naughty! Shouldn't be doing GetDecl in parser...
- Repeat
- Local id$=ParseIdent()
- decl=scope.FindDecl( id )
- If Not decl Err "Identifier '"+id+"' not found."
- If Not CParse( "." ) Exit
- scope=TScopeDecl( decl )
- If Not scope Or TFuncDecl( scope ) Err "Invalid scope '"+id+"'."
- Forever
- _env=Null '/naughty
- _module.InsertDecl New TAliasDecl.Create( ident,decl,attrs )
- Until Not CParse(",")
- Case "module"
- Local m:String = ParseModuleDecl(_toke, attrs)
- If m.ToLower() <> opt_modulename Then
- Err "Module does not match commandline module"
- End If
- 'sanitize, remove non-allowed chars
- _module.munged = TStringHelper.Sanitize(m)
- Case "nodebug"
- mainFunc.attrs :| DECL_NODEBUG
- attrs :| DECL_NODEBUG
- NextToke
- Case "strict"
- If _module.attrs & (MODULE_STRICT | MODULE_SUPERSTRICT) Then
- Err "Strict or SuperStrict already specified"
- End If
- _module.attrs :| MODULE_STRICT
- nextToke
- Case "superstrict"
- If _module.attrs & (MODULE_STRICT | MODULE_SUPERSTRICT) Then
- Err "Strict or SuperStrict already specified"
- End If
- _module.attrs :| MODULE_SUPERSTRICT
- opt_issuperstrict = True
- nextToke
- Case "moduleinfo"
- NextToke
- Local info:String = ParseStringLit()
- _module.modInfo.AddLast(info)
- Default
- Exit
- End Select
- Wend
- ' app code
- PushBlock(mainFunc)
- 'Parse main app
- attrs = ParseCurrentFile(path, attrs)
- PopBlock
- End Method
- Method ParseModule()
- End Method
- Method Create:TParser( toker:TToker,app:TAppDecl )
- _toke="~n"
- _toker=toker
- _app=app
- SetErr
- NextToke
- Return Self
- End Method
- End Type
- Function Eval$( toker:TToker,_type:TType )
- Local src$
- While toker.Toke() And toker.Toke()<>"'" And toker.Toke()<>"~n" And toker.Toke()<>"~r"
- src:+toker.Toke()
- toker.NextToke()
- Wend
- Local t:String=EvalS( src,_type )
- Return t
- End Function
- Function PreProcessNextToke$(toker:TToker)
- Repeat
- toker.NextToke()
- Until toker.tokeType()<>TOKE_SPACE Or toker.Toke().Endswith("~n")
- Return toker._toke
- End Function
- Function PreProcess$( path$ )
- Local ifnest:Int,con:Int=1,line:Int,source:TStringList=New TStringList
- Local toker:TToker=New TToker.Create( path,LoadText( path ), True )
- PreProcessNextToke(toker)
- Repeat
- If line
- source.AddLast "~n"
- While toker.Toke() And Not toker.Toke().Endswith("~n") And toker.TokeType()<>TOKE_LINECOMMENT
- PreProcessNextToke(toker)
- Wend
- If Not toker.Toke() Exit
- PreProcessNextToke(toker)
- EndIf
-
- line :+ 1
- ' catch up with any skipped lines
- While line < toker._line - 1
- line:+1
- source.AddLast "~n"
- Wend
- If toker.TokeType()=TOKE_SPACE And Not toker.Toke().Endswith("~n") PreProcessNextToke(toker)
- If toker.Toke()<>"?"
- If con
- Local textline$
- While toker.Toke() And toker.Toke()<>"~n" And toker.TokeType()<>TOKE_LINECOMMENT
- Local toke$=toker.Toke()
- textline:+toke
- toker.NextToke()
- Wend
- If textline Then
- source.AddLast textline
- EndIf
- EndIf
- Continue
- EndIf
- Local stm$= PreProcessNextToke(toker).ToLower()
- 'toker.NextToke()
- Local isNot:Int = False
- If stm = "not" Then
- If toker.TokeType()=TOKE_SPACE PreProcessNextToke(toker)
- stm = toker.Toke().ToLower()
- isNot = True
- End If
- 'If stm="end" Or stm="else"
- ' If toker.TokeType()=TOKE_SPACE toker.NextToke()
- ' If toker.Toke().ToLower()="if"
- ' toker.NextToke()
- ' stm:+"if"
- ' EndIf
- 'EndIf
- Rem
- Debug True if program is being compiled in debug mode.
- Threaded True if program is being compiled in threaded mode.
- Win32 True if program is being compiled for the Windows operating system.
- MacOS True if program is being compiled for the MacOS operating system.
- Linux True if program is being compiled for the Linux operating system.
- X86 True if program is being compiled for the Intel CPU.
- PPC True if program is being compiled for the PowerPC CPU.
- MacOSX86 True if program is being compiled for an Intel Mac.
- MacOSPPC True if program is being compiled for a PowerPC Mac.
- BigEndian True if program is being compiled for a big endian CPU.
- LittleEndian
- End Rem
- Select stm
- Case "~r", "~n"
- 'ifnest = 0
- con = 1
- Default
- ' test for EOF
- If Not toker.Toke() Exit
- con = 0
- Try
- If Eval( toker,New TIntType ) = "1" con = 1
- Catch error:String
- con = 0
- End Try
- Rem
- Case "macos", "macosx86", "x86", "littleendian", "bigendian"
- con = 1
- ' If con=ifnest
- ' If Eval( toker,TType.intType ) con:+1
- ' EndIf
- '
- ifnest = 1
- ' Case "rem"
- '
- ' ifnest:+1
- Case "threaded", "win32", "linux", "ppc", "win32x86", "linuxx86", "macosppc"
- If isNot Then
- con = 1
- Else
- con = 0
- End If
- ifnest = 1
- Case "else","elseif"
- If Not ifnest Err "#Else without #If"
- If con=ifnest
- con=-con
- Else If con=ifnest-1
- If stm="elseif"
- If Eval( toker,TType.intType ) con:+1
- Else
- con:+1
- EndIf
- EndIf
- Case "end","endif"
- If Not ifnest Err "#End without #If"
- ifnest:-1
- If con<0 con=-con
- If ifnest<con con=ifnest
- ' Case "print"
- ' If con=ifnest
- ' TODO
- 'Print ReplaceEnvTags( Eval( toker,TType.stringType ) )
- ' EndIf
- ' Case "error"
- ' If con=ifnest
- ' TODO
- 'Err ReplaceEnvTags( Eval( toker,TType.stringType ) )
- ' EndIf
- Default
- Err "Unrecognized preprocessor directive '"+stm+"'."
- End Rem
- End Select
- Forever
- Return source.Join( "" )
- End Function
- Function ParseModule:TModuleDecl( path$,app:TAppDecl )
- 'Local source$=PreProcess( path )
- Local source:String = LoadText(path)
- Local toker:TToker=New TToker.Create( path,source )
- Local parser:TParser=New TParser.Create( toker,app )
- parser.ParseMain
- Return parser._module
- End Function
- '***** PUBLIC API ******
- Function ParseApp:TAppDecl( path$ )
- Local app:TAppDecl=New TAppDecl
- _appInstance = app
- Local source$=PreProcess( path )
- 'Local source:String = LoadString(path)
- Local toker:TToker=New TToker.Create( path,source )
- Local parser:TParser=New TParser.Create( toker,app )
- parser.ParseMain
- Return app
- End Function
- Function MungModuleName:String(ident:Object)
- Local mung:String
- If String(ident) Then
- Local id:String = String(ident)
- mung = "__bb_" + id + "_" + id[id.Find(".") + 1..]
- Else
- Local mdecl:TModuleDecl = TModuleDecl(ident)
- If mdecl Then
- Local id:String = mdecl.ident
- Local dir:String = ExtractDir(mdecl.filepath).ToLower()
- dir = dir[dir.findLast("/") + 1..]
- If dir.EndsWith(".mod") Then
- dir = ""
- Else
- dir :+ "_"
- End If
- mung = "__bb_" + id + "_" + dir + id[id.Find(".") + 1..]
- End If
- End If
- 'return sanitized, remove non-allowed chars
- Return TStringHelper.Sanitize(mung)
- End Function
- Function EvalS$( source$,ty:TType )
- Local env:TScopeDecl=New TScopeDecl
- Rem
- Debug True if program is being compiled in debug mode.
- Threaded True if program is being compiled in threaded mode.
- Win32 True if program is being compiled for the Windows operating system.
- MacOS True if program is being compiled for the MacOS operating system.
- Linux True if program is being compiled for the Linux operating system.
- X86 True if program is being compiled for the Intel CPU.
- PPC True if program is being compiled for the PowerPC CPU.
- MacOSX86 True if program is being compiled for an Intel Mac.
- MacOSPPC True if program is being compiled for a PowerPC Mac.
- BigEndian True if program is being compiled for a big endian CPU.
- LittleEndian
- End Rem
- ' debug/release
- env.InsertDecl New TConstDecl.Create( "debug",New TIntType,New TConstExpr.Create( New TIntType,opt_debug ),0 )
- 'env.InsertDecl New TConstDecl.Create( "release",TType.intType,New TConstExpr.Create( TType.intType,opt_release ),0 )
- ' threaded
- env.InsertDecl New TConstDecl.Create( "threaded",New TIntType,New TConstExpr.Create( New TIntType,opt_threaded ),0 )
- ' macos
- env.InsertDecl New TConstDecl.Create( "macos",New TIntType,New TConstExpr.Create( New TIntType,opt_platform="macos" Or opt_platform="osx" Or opt_platform="ios"),0 )
- env.InsertDecl New TConstDecl.Create( "macosx86",New TIntType,New TConstExpr.Create( New TIntType,(opt_platform="macos" Or opt_platform="osx" Or opt_platform="ios") And opt_arch="x86"),0 )
- env.InsertDecl New TConstDecl.Create( "macosppc",New TIntType,New TConstExpr.Create( New TIntType,(opt_platform="macos" Or opt_platform="osx") And opt_arch="ppc"),0 )
- env.InsertDecl New TConstDecl.Create( "macosx64",New TIntType,New TConstExpr.Create( New TIntType,(opt_platform="macos" Or opt_platform="osx" Or opt_platform="ios") And opt_arch="x64"),0 )
- ' osx
- env.InsertDecl New TConstDecl.Create( "osx",New TIntType,New TConstExpr.Create( New TIntType,opt_platform="macos" Or opt_platform="osx" ),0 )
- env.InsertDecl New TConstDecl.Create( "osxx86",New TIntType,New TConstExpr.Create( New TIntType,(opt_platform="macos" Or opt_platform="osx") And opt_arch="x86"),0 )
- env.InsertDecl New TConstDecl.Create( "osxppc",New TIntType,New TConstExpr.Create( New TIntType,(opt_platform="macos" Or opt_platform="osx") And opt_arch="ppc"),0 )
- env.InsertDecl New TConstDecl.Create( "osxx64",New TIntType,New TConstExpr.Create( New TIntType,(opt_platform="macos" Or opt_platform="osx") And opt_arch="x64"),0 )
- ' ios
- env.InsertDecl New TConstDecl.Create( "ios",New TIntType,New TConstExpr.Create( New TIntType,opt_platform="ios" ),0 )
- env.InsertDecl New TConstDecl.Create( "iosx86",New TIntType,New TConstExpr.Create( New TIntType,opt_platform="ios" And opt_arch="x86"),0 )
- env.InsertDecl New TConstDecl.Create( "iosx64",New TIntType,New TConstExpr.Create( New TIntType,opt_platform="ios" And opt_arch="x64"),0 )
- env.InsertDecl New TConstDecl.Create( "iosarmv7",New TIntType,New TConstExpr.Create( New TIntType,opt_platform="ios" And opt_arch="armv7"),0 )
- env.InsertDecl New TConstDecl.Create( "iosarm64",New TIntType,New TConstExpr.Create( New TIntType,opt_platform="ios" And opt_arch="arm64"),0 )
- ' windows
- env.InsertDecl New TConstDecl.Create( "win32",New TIntType,New TConstExpr.Create( New TIntType,opt_platform="win32" ),0 )
- env.InsertDecl New TConstDecl.Create( "win32x86",New TIntType,New TConstExpr.Create( New TIntType,opt_platform="win32" And opt_arch="x86"),0 )
- env.InsertDecl New TConstDecl.Create( "win32x64",New TIntType,New TConstExpr.Create( New TIntType,(opt_platform="win64" And opt_arch="x64") Or (opt_platform="win32" And opt_arch="x64")),0 )
- env.InsertDecl New TConstDecl.Create( "win64",New TIntType,New TConstExpr.Create( New TIntType,(opt_platform="win64" And opt_arch="x64") Or (opt_platform="win32" And opt_arch="x64")),0 )
- ' linux
- env.InsertDecl New TConstDecl.Create( "linux",New TIntType,New TConstExpr.Create( New TIntType,(opt_platform="linux" Or opt_platform="android" Or opt_platform="raspberrypi")),0 )
- env.InsertDecl New TConstDecl.Create( "linuxx86",New TIntType,New TConstExpr.Create( New TIntType,(opt_platform="linux" Or opt_platform="android") And opt_arch="x86"),0 )
- env.InsertDecl New TConstDecl.Create( "linuxx64",New TIntType,New TConstExpr.Create( New TIntType,(opt_platform="linux" Or opt_platform="android") And opt_arch="x64"),0 )
- env.InsertDecl New TConstDecl.Create( "linuxarm",New TIntType,New TConstExpr.Create( New TIntType, ((opt_platform="android" Or opt_platform="linux") And (opt_arch="arm" Or opt_arch="armeabi" Or opt_arch="armeabiv7a" Or opt_arch="arm64v8a")) Or (opt_platform="raspberrypi" And opt_arch="arm")),0 )
- ' android
- env.InsertDecl New TConstDecl.Create( "android",New TIntType,New TConstExpr.Create( New TIntType,opt_platform="android" ),0 )
- env.InsertDecl New TConstDecl.Create( "androidx86",New TIntType,New TConstExpr.Create( New TIntType,opt_platform="android" And opt_arch="x86"),0 )
- env.InsertDecl New TConstDecl.Create( "androidx64",New TIntType,New TConstExpr.Create( New TIntType,opt_platform="android" And opt_arch="x64"),0 )
- env.InsertDecl New TConstDecl.Create( "androidarm",New TIntType,New TConstExpr.Create( New TIntType,opt_platform="android" And (opt_arch="arm" Or opt_arch="armeabi" Or opt_arch="armeabiv7a" Or opt_arch="arm64v8a") ),0 )
- env.InsertDecl New TConstDecl.Create( "androidarmeabi",New TIntType,New TConstExpr.Create( New TIntType,opt_platform="android" And opt_arch="armeabi"),0 )
- env.InsertDecl New TConstDecl.Create( "androidarmeabiv7a",New TIntType,New TConstExpr.Create( New TIntType,opt_platform="android" And opt_arch="armeabiv7a"),0 )
- env.InsertDecl New TConstDecl.Create( "androidarm64v8a",New TIntType,New TConstExpr.Create( New TIntType,opt_platform="android" And opt_arch="arm64v8a"),0 )
- ' raspberrypi - ARM only
- env.InsertDecl New TConstDecl.Create( "raspberrypi",New TIntType,New TConstExpr.Create( New TIntType,opt_platform="raspberrypi" And opt_arch="arm"),0 )
- env.InsertDecl New TConstDecl.Create( "raspberrypiARM",New TIntType,New TConstExpr.Create( New TIntType,opt_platform="raspberrypi" And opt_arch="arm"),0 )
- ' emscripten
- env.InsertDecl New TConstDecl.Create( "emscripten",New TIntType,New TConstExpr.Create( New TIntType,opt_platform="emscripten" ),0 )
- env.InsertDecl New TConstDecl.Create( "emscriptenjs",New TIntType,New TConstExpr.Create( New TIntType,opt_platform="emscripten" And opt_arch="js"),0 )
- ' arch
- env.InsertDecl New TConstDecl.Create( "ppc",New TIntType,New TConstExpr.Create( New TIntType,opt_arch="ppc" ),0 )
- env.InsertDecl New TConstDecl.Create( "x86",New TIntType,New TConstExpr.Create( New TIntType,opt_arch="x86" ),0 )
- env.InsertDecl New TConstDecl.Create( "x64",New TIntType,New TConstExpr.Create( New TIntType,opt_arch="x64" ),0 )
- env.InsertDecl New TConstDecl.Create( "arm",New TIntType,New TConstExpr.Create( New TIntType,opt_arch="arm" Or opt_arch="armeabi" Or opt_arch="armeabiv7a" Or opt_arch="arm64v8a" ),0 )
- env.InsertDecl New TConstDecl.Create( "armeabi",New TIntType,New TConstExpr.Create( New TIntType,opt_arch="armeabi" ),0 )
- env.InsertDecl New TConstDecl.Create( "armeabiv7a",New TIntType,New TConstExpr.Create( New TIntType,opt_arch="armeabiv7a" ),0 )
- env.InsertDecl New TConstDecl.Create( "arm64v8a",New TIntType,New TConstExpr.Create( New TIntType,opt_arch="arm64v8a" ),0 )
- env.InsertDecl New TConstDecl.Create( "js",New TIntType,New TConstExpr.Create( New TIntType,opt_arch="js" ),0 )
- env.InsertDecl New TConstDecl.Create( "armv7",New TIntType,New TConstExpr.Create( New TIntType,opt_arch="armv7" ),0 )
- env.InsertDecl New TConstDecl.Create( "arm64",New TIntType,New TConstExpr.Create( New TIntType,opt_arch="arm64" ),0 )
- env.InsertDecl New TConstDecl.Create( "ptr32",New TIntType,New TConstExpr.Create( New TIntType,opt_arch="x86" Or opt_arch="ppc" Or opt_arch="armv7" Or opt_arch="arm" Or opt_arch="armeabi" Or opt_arch="armeabiv7a" ),0 )
- env.InsertDecl New TConstDecl.Create( "ptr64",New TIntType,New TConstExpr.Create( New TIntType,opt_arch="x64" Or opt_arch="arm64" Or opt_arch="arm64v8a" ),0 )
- ' endian
- env.InsertDecl New TConstDecl.Create( "bigendian",New TIntType,New TConstExpr.Create( New TIntType,opt_arch="ppc" ),0 )
- env.InsertDecl New TConstDecl.Create( "littleendian",New TIntType,New TConstExpr.Create( New TIntType,opt_arch<>"ppc" ),0 )
- ' opengles target platform
- env.InsertDecl New TConstDecl.Create( "opengles",New TIntType,New TConstExpr.Create( New TIntType, opt_platform="android" Or opt_platform="raspberrypi" Or opt_platform="emscripten" Or opt_platform="ios" Or (opt_platform="linux" And opt_arch="arm") ),0 )
- ' musl - linux only
- env.InsertDecl New TConstDecl.Create( "musl",New TIntType,New TConstExpr.Create( New TIntType,(opt_musl And (opt_platform="linux" Or opt_platform="android" Or opt_platform="raspberrypi"))),0 )
-
- ' new compiler
- env.InsertDecl New TConstDecl.Create( "bmxng",New TIntType,New TConstExpr.Create( New TIntType, True ),0 )
- ' env.InsertDecl New TConstDecl.Create( "LANG",TType.stringType,New TConstExpr.Create( TType.stringType,ENV_LANG ),0 )
- ' env.InsertDecl New TConstDecl.Create( "TARGET",TType.stringType,New TConstExpr.Create( TType.stringType,ENV_TARGET ),0 )
- ' env.InsertDecl New TConstDecl.Create( "CONFIG",TType.stringType,New TConstExpr.Create( TType.stringType,ENV_CONFIG ),0 )
- PushEnv env
- Local toker:TToker=New TToker.Create( "",source )
- Local parser:TParser=New TParser.Create( toker,Null )
- Local expr:TExpr=parser.ParseExpr()
- expr=expr.Semant()
- If ty expr=expr.Cast( ty )
- Local val$=expr.Eval()
- PopEnv
- Return val
- End Function
- Type TCastDets
- Field name:String
- Field retType:String
- Field noGen:Int
- Field args:String[0]
- Field api:String
-
- End Type
|