C ensureNamespace: #Syntax. "This defines C syntax structure in a sufficient manner for generation, but not parsing (yet)." C Syntax addPrototype: #Node derivedFrom: {Cloneable}. node@(C Syntax Node traits) returnType [Types C Void]. node@(C Syntax Node traits) walk: block "A depth-first do:-style iteration through SyntaxNodes; a code/tree-walker." [ block applyWith: node. ]. node@(C Syntax Node traits) transformBy: block "Transforms the tree's nodes in-place by the block closure. Only transformChildren: needs to be overridden." [ (block applyWith: node) transformChildren: block ]. node@(C Syntax Node traits) transformChildren: block "Act on each of a node's children in-place. This gets overridden for the various compound node types." [ node ]. _@(C Syntax Node traits) precedence [0]. "The precedence protocol just returns a number that can be compared to determine precedence among the same type of Syntax Nodes." "For the root Node type: when in doubt, force the use of parentheses." n@(C Syntax Node traits) nodeCount "Answer the number of nodes in this tree, analogous to the size of the tree." [| count | count: 0. n walk: [| :_ | count: count + 1]. count ]. C Syntax addPrototype: #Direct derivedFrom: {C Syntax Node}. "Directly inserts its String contents. Useful for preprocessor directives." C Syntax Direct addSlot: #contents valued: ''. d@(C Syntax Direct traits) for: contents [| newD | newD: d clone. newD contents: contents. newD ]. C Syntax addPrototype: #Comment derivedFrom: {C Syntax Direct}. "Directly inserts a lot of text, surrounded by /* */." C Syntax addPrototype: #Statement derivedFrom: {C Syntax Node}. _@(C Syntax Statement traits) returnType [Types C Void]. C Syntax addPrototype: #Break derivedFrom: {C Syntax Statement}. C Syntax addPrototype: #Continue derivedFrom: {C Syntax Statement}. C Syntax addPrototype: #Label derivedFrom: {C Syntax Statement}. C Syntax Label addSlot: #name. C Syntax Label addSlot: #statement. statement@(C Syntax Statement traits) labelled: label [| newL | newL: C Syntax Label clone. newL name: label. newL statement: statement. newL ]. node@(C Syntax Label traits) walk: block [ resend. node statement walk: block ]. C Syntax addPrototype: #Goto derivedFrom: {C Syntax Statement}. C Syntax Goto addSlot: #label. g@(C Syntax Goto traits) to: label [| newG | newG: g clone. newG label: label. newG ]. C Syntax addPrototype: #Return derivedFrom: {C Syntax Statement}. C Syntax Return addSlot: #result. node@(C Syntax Return traits) walk: block [ resend. node result walk: block ]. node@(C Syntax Return traits) of: value [| newN | newN: node clone. newN result: value. newN ]. C Syntax addPrototype: #ControlStatement derivedFrom: {C Syntax Statement}. C Syntax addPrototype: #IfThenElse derivedFrom: {C Syntax ControlStatement}. C Syntax IfThenElse addSlot: #condition. C Syntax IfThenElse addSlot: #trueBody. C Syntax IfThenElse addSlot: #falseBody. ite@(C Syntax IfThenElse traits) on: condition then: trueBody else: falseBody [| newITE | newITE: ite clone. newITE condition: condition. newITE trueBody: trueBody. newITE falseBody: falseBody. newITE ]. ite@(C Syntax IfThenElse traits) on: condition then: trueBody [| newITE | newITE: ite clone. newITE condition: condition. newITE trueBody: trueBody. newITE falseBody: Nil. newITE ]. node@(C Syntax IfThenElse traits) walk: block [ resend. node condition walk: block. node trueBody walk: block. node falseBody walk: block. ]. node@(C Syntax IfThenElse traits) transformChildren: block [ node condition: (block applyWith: node condition). node trueBody: (block applyWith: node trueBody). node falseBody: (block applyWith: node falseBody). node ]. node@(C Syntax IfThenElse traits) test: cond ifTrue: then ifFalse: else [| newN | newN: node clone. newN condition: cond. newN trueBody: then. newN falseBody: else. newN ]. C Syntax addPrototype: #Empty derivedFrom: {C Syntax Statement}. C Syntax addPrototype: #Block derivedFrom: {C Syntax ControlStatement}. C Syntax Block addSlot: #declarations valued: Dictionary newEmpty. C Syntax Block addSlot: #statements valued: {}. node@(C Syntax Block traits) newEmpty [| newB | newB: node clone. newB declarations: newB declarations newEmpty. newB statements: newB statements newEmpty. newB ]. node@(C Syntax Block traits) walk: block [ resend. node declarations do: [| :decl | decl walk: block]. node statements do: [| :st | st walk: block]. ]. node@(C Syntax Block traits) transformChildren: block [ node declarations infect: [| :decl | decl transformBy: block]. node statements infect: [| :st | st transformBy: block]. node ]. node@(C Syntax Block traits) define: name as: value type: type [| newDef | newDef: C Syntax VariableDef clone. newDef name: name. newDef init: value. newDef type: type. node declarations at: name put: newDef ]. node@(C Syntax Block traits) define: name as: value [node define: name as: value type: value type]. node@(C Syntax Block traits) define: name type: type [node define: name as: Nil type: type]. node@(C Syntax Block traits) variableNamed: name [ node declarations at: name ifAbsent: [] ]. C Syntax addPrototype: #Expression derivedFrom: {C Syntax Node}. _@(C Syntax Expression traits) returnType [Types C LongInt]. C Syntax addPrototype: #VariableRef derivedFrom: {C Syntax Expression}. C Syntax VariableRef addSlot: #name. C Syntax VariableRef addSlot: #cType. var@(C Syntax VariableRef traits) returnType [var cType]. _@(C Syntax VariableRef traits) precedence [16]. "Never needs parentheses to disambiguate." node@(C Syntax VariableRef traits) of: name type: type [| newN | newN: node clone. newN name: name. newN cType: type. newN ]. C Syntax addPrototype: #GlobalRef derivedFrom: {C Syntax VariableRef}. C Syntax addPrototype: #ConstantRef derivedFrom: {C Syntax VariableRef}. C Syntax addPrototype: #FunctionRef derivedFrom: {C Syntax VariableRef}. C Syntax addPrototype: #Conditional derivedFrom: {C Syntax Expression}. C Syntax Conditional addSlot: #condition. C Syntax Conditional addSlot: #trueBody. C Syntax Conditional addSlot: #falseBody. cond@(C Syntax Conditional traits) on: condition then: then else: else [| newCond | newCond: cond clone. newCond condition: condition. newCond trueBody: then. newCond falseBody: else. newCond ]. cond@(C Syntax Conditional traits) on: condition then: then [| newCond | newCond: cond clone. newCond condition: condition. newCond trueBody: then. newCond falseBody: Nil. newCond ]. c@(C Syntax Conditional traits) as: ite@(C Syntax IfThenElse traits) [| newN | newN: ite clone. newN condition: c condition. newN trueBody: c trueBody. newN falseBody: c falseBody. newN ]. ite@(C Syntax IfThenElse traits) as: c@(C Syntax Conditional traits) [| newN | newN: c clone. newN condition: ite condition. newN trueBody: ite trueBody. newN falseBody: ite falseBody. newN ]. node@(C Syntax Conditional traits) walk: block [ resend. node trueBody walk: block. node falseBody walk: block. ]. cond@(C Syntax Conditional traits) returnType [cond then returnType]. "TODO: support a union or check between the then and else returnTypes." _@(C Syntax Conditional traits) precedence [3]. C Syntax addPrototype: #ForLoop derivedFrom: {C Syntax ControlStatement}. C Syntax ForLoop addSlot: #init. C Syntax ForLoop addSlot: #stopTest. C Syntax ForLoop addSlot: #step. C Syntax ForLoop addSlot: #body. loop@(C Syntax ForLoop traits) from: start while: stopTest by: step doing: body [| newLoop | newLoop: loop clone. newLoop init: start. newLoop stopTest: stopTest. newLoop step: step. newLoop body: body. newLoop ]. node@(C Syntax ForLoop traits) walk: block [ resend. node init walk: block. node stopTest walk: block. node step walk: block. node body walk: block. ]. C Syntax addPrototype: #WhileLoop derivedFrom: {C Syntax ControlStatement}. C Syntax WhileLoop addSlot: #test. C Syntax WhileLoop addSlot: #body. loop@(C Syntax WhileLoop traits) doing: body while: test [| newLoop | newLoop: loop clone. newLoop body: body. newLoop test: test. newLoop ]. node@(C Syntax WhileLoop traits) walk: block [ resend. node test walk: block. node body walk: block. ]. C Syntax addPrototype: #DoWhileLoop derivedFrom: {C Syntax WhileLoop}. C Syntax addPrototype: #Assignment derivedFrom: {C Syntax Expression}. C Syntax Assignment addSlot: #name. C Syntax Assignment addSlot: #value. node@(C Syntax Assignment traits) of: value into: name [| newN | newN: node clone. newN name: name. newN value: value. newN ]. node@(C Syntax Assignment traits) walk: block [ resend. node value walk: block. ]. node@(C Syntax Assignment traits) returnType [node value returnType]. _@(C Syntax Assignment traits) precedence [2]. C Syntax addPrototype: #Unary derivedFrom: {C Syntax Expression}. C Syntax Unary addSlot: #op. C Syntax Unary addSlot: #argument. node@(C Syntax Unary traits) walk: block [ resend. node argument walk: block. ]. node@(C Syntax Unary traits) calling: selector on: argument [| newN | newN: node clone. newN op: selector. newN argument: argument. newN ]. node@(C Syntax Unary traits) on: argument [| newN | newN: node clone. newN argument: argument. newN ]. C Syntax addPrototype: #Test derivedFrom: {C Syntax Expression}. _@(C Syntax Test traits) returnType [Types Member of: {C True. C False}]. C Syntax addPrototype: #Prefix derivedFrom: {C Syntax Unary}. _@(C Syntax Prefix traits) precedence [14]. C Syntax addPrototype: #Not derivedFrom: {C Syntax Prefix. C Syntax Test}. C Syntax Not addImmutableSlot: #op valued: #'!'. node@(C Syntax Expression traits) not "A quick expression for boolean NOT." [C Syntax Not on: node]. C Syntax addPrototype: #Postfix derivedFrom: {C Syntax Unary}. _@(C Syntax Postfix traits) precedence [15]. C Syntax addPrototype: #Infix derivedFrom: {C Syntax Expression}. C Syntax Infix addSlot: #op. C Syntax Infix addSlot: #args valued: {}. node@(C Syntax Infix traits) forOp: selector [| newNode | newNode: node clone. newNode op: selector. newNode ]. node@(C Syntax Infix traits) on: args [| newNode | newNode: node clone. newNode args: args. newNode ]. _@(C Syntax Infix traits) precedence [13]. "Precedence for Infixes much more complicated than one value, but this works as well as I care about." node@(C Syntax Infix traits) walk: block [ resend. node args do: [| :arg | arg walk: block]. ]. node@(C Syntax Infix traits) calling: selector on: arguments [| newN | newN: node clone. newN op: selector. newN args: arguments. newN ]. C Syntax addPrototype: #Comparison derivedFrom: {C Syntax Infix. C Syntax Test}. _@(C Syntax Comparison traits) precedence [10]. C Syntax addSlot: #Equals valued: (C Syntax Comparison forOp: #==). C Syntax addSlot: #NotEquals valued: (C Syntax Comparison forOp: #'!='). C Syntax addPrototype: #BitLogic derivedFrom: {C Syntax Infix}. _@(C Syntax BitLogic traits) precedence [8]. C Syntax addPrototype: #OpAssignment derivedFrom: {C Syntax Assignment}. C Syntax OpAssignment addSlot: #operation. C Syntax OpAssignment traits addSlot: #selectors valued: {#+. #-. #*. #/. #<<. #>>}. _@(C Syntax OpAssignment traits) precedence [1]. node@(C Syntax OpAssignment traits) of: opName into: name by: value [| newN | newN: node clone. newN name: name. newN value: value. newN operation: opName. newN ]. C Syntax addPrototype: #SizeOf derivedFrom: {C Syntax Prefix}. size@(C Syntax SizeOf traits) of: exp [| newS | newS: size clone. newS expression: exp newS ]. C Syntax addPrototype: #Cast derivedFrom: {C Syntax Prefix}. C Syntax Cast addSlot: #targetType. node@(C Syntax Cast traits) of: argument to: type [| newN | newN: node clone. newN argument: argument. newN targetType: type. newN ]. node@(C Syntax Cast traits) returnType [node targetType]. C Syntax addPrototype: #Invocation derivedFrom: {C Syntax Expression}. _@(C Syntax Invocation traits) precedence [15]. C Syntax addPrototype: #Subscript derivedFrom: {C Syntax Invocation}. C Syntax Subscript addSlot: #array. C Syntax Subscript addSlot: #subscripts valued: {}. node@(C Syntax Subscript traits) returnType [node array elementType]. node@(C Syntax Expression traits) at: subscript [| newSub | newSub: C Syntax Subscript clone. newSub array: node. newSub subscripts: {subscript}. newSub ]. node@(C Syntax Subscript traits) at: subscript "Add in the subscript." [ node subscripts: (node subscripts copyWith: subscript). node ]. node@(C Syntax Subscript traits) walk: block [ resend. node subscripts do: [| :sub | sub walk: block]. ]. C Syntax addPrototype: #FunctionCall derivedFrom: {C Syntax Invocation}. C Syntax FunctionCall addSlot: #function. C Syntax FunctionCall addSlot: #arguments valued: {}. c@(C Syntax FunctionCall traits) applying: function to: arguments [| newC | newC: c clone. newC function: function. newC arguments: arguments. newC ]. node@(C Syntax FunctionCall traits) walk: block [ resend. node arguments do: [| :arg | arg walk: block]. ]. C Syntax addPrototype: #Select derivedFrom: {C Syntax Invocation}. C Syntax Select addSlot: #struct. C Syntax Select addSlot: #type. C Syntax Select addSlot: #member. select@(C Syntax Select traits) from: struct type: type named: member [| newS | newS: select clone. newS struct: struct. newS type: type. newS member: member. newS ]. select@(C Syntax Select traits) returnType [ (select type elementSpecs detect: [| :spec | spec cName = select member] ifNone: [error: 'Structure member not found.']) type ]. C Syntax addPrototype: #PointAt derivedFrom: {C Syntax Select}. C Syntax addPrototype: #Definition derivedFrom: {C Syntax Statement}. C Syntax Definition addSlot: #type. C Syntax Definition addSlot: #name. C Syntax addPrototype: #VariableDef derivedFrom: {C Syntax Definition}. C Syntax VariableDef addSlot: #init. node@(C Syntax VariableDef traits) of: name init: value type: type [| newN | newN: node clone. newN name: name. newN init: value. newN type: type. newN ]. cVar@(C Syntax VariableDef traits) of: name [ cVar of: name init: Nil type: Nil ]. C Syntax addPrototype: #GlobalDef derivedFrom: {C Syntax VariableDef}. C Syntax GlobalDef addSlot: #isStatic valued: False. C Syntax GlobalDef addSlot: #isExported valued: False. g@(C Syntax GlobalDef traits) export [ g isExported: True. g ]. g@(C Syntax GlobalDef traits) static [ g isStatic: True. g ]. C Syntax addPrototype: #ConstantDef derivedFrom: {C Syntax GlobalDef}. C Syntax addPrototype: #FunctionDef derivedFrom: {C Syntax Definition}. C Syntax FunctionDef addSlot: #args valued: {}. "A Sequence of VariableDef's." C Syntax FunctionDef addSlot: #isStatic valued: False. C Syntax FunctionDef addSlot: #isExported valued: False. C Syntax FunctionDef addSlot: #isInline valued: False. C Syntax FunctionDef addSlot: #body valued: C Syntax Block. f@(C Syntax FunctionDef traits) of: name on: args type: type [| newF | newF: f clone. newF name: name. newF args: args. newF type: type. newF ]. node@(C Syntax FunctionDef traits) walk: block [ resend. node body walk: block ]. f@(C Syntax FunctionDef traits) variableNamed: name [ f args detect: [| :arg | arg name = name] ]. f@(C Syntax FunctionDef traits) export [ f isExported: True. f ]. f@(C Syntax FunctionDef traits) static [ f isStatic: True. f ]. f@(C Syntax FunctionDef traits) inline [ f isInline: True. f ]. C Syntax addPrototype: #StructDef derivedFrom: {C Syntax Definition}. C Syntax StructDef addSlot: #elements valued: {}. C Syntax addPrototype: #CaseSwitch derivedFrom: {C Syntax ControlStatement}. C Syntax CaseSwitch addSlot: #value. C Syntax CaseSwitch addSlot: #cases valued: {}. C Syntax CaseSwitch addSlot: #otherwise. C Syntax addPrototype: #AddressOf derivedFrom: {C Syntax Prefix}. C Syntax AddressOf addSlot: #expression. addr@(C Syntax AddressOf traits) of: exp [| newA | newA: addr clone. newA expression: exp. newA ]. _@(C Syntax AddressOf traits) argumentType [Types C Stored]. n@(C Syntax AddressOf traits) returnType [Types C Pointer to: n expression returnType]. node@(C Syntax AddressOf traits) walk: block [ resend. node expression walk: block ]. C Syntax addPrototype: #Dereference derivedFrom: {C Syntax Prefix}. C Syntax Dereference addSlot: #pointer. deref@(C Syntax Dereference traits) of: pointer [| newD | newD: deref clone. newD pointer: pointer. newD ]. _@(C Syntax Dereference traits) argumentType [Types C Pointer]. n@(C Syntax Dereference traits) returnType [n pointer target type]. C Syntax addPrototype: #Literal derivedFrom: {C Syntax Expression}. C Syntax Literal addSlot: #value. "A Slate object for the literal." _@(C Syntax Literal traits) returnType [Types C UnsignedLongInt]. _@(C Syntax Literal traits) precedence [16]. l@(C Syntax Literal traits) as: _@(C Syntax Literal traits) [ l ]. C Syntax addPrototype: #ArrayLiteral derivedFrom: {C Syntax Expression}. C Syntax ArrayLiteral addSlot: #elements valued: {}. _@(C Syntax ArrayLiteral traits) returnType [Types C Array]. _@(C Syntax ArrayLiteral traits) precedence [16]. a@(Sequence traits) as: _@(C Syntax Literal traits) [| newN | newN: C Syntax ArrayLiteral clone. newN elements: (a as: newN elements). newN ]. a@(C Syntax ArrayLiteral traits) as: _@(C Syntax Literal traits) [ a ]. a@(C Syntax ArrayLiteral traits) as: _@(C Syntax ArrayLiteral traits) [ a ]. C Syntax addPrototype: #Null derivedFrom: {C Syntax Literal}. C Syntax Null value: Nil. _@(C Syntax Null traits) returnType [Types C Void pointer]. _@Nil as: _@(C Syntax Literal) [ C Syntax Null ]. C Syntax addPrototype: #String derivedFrom: {C Syntax Literal}. s@(String traits) as: _@(C Syntax Literal traits) [| newN | newN: C Syntax String clone. newN value: s. newN ]. _@(C Syntax String traits) returnType [Types C String]. C Syntax addPrototype: #Character derivedFrom: {C Syntax Literal}. c@(Character traits) as: _@(C Syntax Literal traits) [| newN | newN: C Syntax Character clone. newN value: c. newN ]. _@(C Syntax Character traits) returnType [Types C LongInt]. C Syntax addPrototype: #Integer derivedFrom: {C Syntax Literal}. n@(Integer traits) as: _@(C Syntax Literal traits) [| newN | newN: C Syntax Integer clone. newN value: n. newN ]. _@(C Syntax Integer traits) returnType [Types C LongInt]. C Syntax addPrototype: #Boolean derivedFrom: {C Syntax Literal}. b@(Boolean traits) as: _@(C Syntax Literal traits) [| newB | newB: C Syntax Boolean clone. newB value: b. newB ]. _@(C Syntax Boolean traits) returnType [Types C UnsignedLongInt]. C Syntax addPrototype: #Float derivedFrom: {C Syntax Literal}. x@(Float traits) as: _@(C Syntax Literal traits) [| newN | newN: C Syntax Float clone. newN value: x. newN ]. _@(C Syntax Float traits) returnType [Types C Float].