-- ThreeC4ModItemImpl.ThreeC4
-- Sturgis, May 9, 1986 5:17:42 pm PDT

-- this module contains the implementation code for ModItemAbGram

Include[ThreeC4BaseDecl, ThreeC4RecFcnDecl, ThreeC4BasicAbTypes, ThreeC4ModItemAbGram];

ThreeC4ModItemImpl: Module =
Begin

for ModuleItem.cedarItems: AbstractProduction[CedarItems]
		let CollectModuleValueTypes[tree, defFileName, context] ←
				CollectModuleValueTypes[CedarItems, defFileName, context]
		let CollectModuleFunctionTypes[tree, defFileName, context] ←
				CollectModuleFunctionTypes[CedarItems, defFileName, context]
		let CollectProductions[tree, first, context] ← 
			<FakeCopyInt[first], FakeCopyContextForConditional[context]>
		let CollectRightSideSymbols[tree, context] ←
				FakeCopyContextForConditional[context]
		let DefFileCode[tree, context, usage] ← <BuildEmptyCode[], FakeUsageCopy[usage]>
		let ImplFileCode[tree, context, usage] ← <BuildEmptyCode[], FakeUsageCopy[usage]>
		let LinkFileCode[tree, context, usage] ← <BuildEmptyCode[], FakeUsageCopy[usage]>
		let SyntaxFileCodes[tree, context] ← <BuildEmptyCode[], BuildEmptyCode[]>
		let TestLinkPresent[tree] ← EqualRopes["True", "False"];
		
for ModuleItem.baseItems: AbstractProduction[BaseItems]
		let CollectModuleValueTypes[tree, defFileName, context] ←
				CollectModuleValueTypes[BaseItems, defFileName, context]
		let CollectModuleFunctionTypes[tree, defFileName, context] ←
				CollectModuleFunctionTypes[BaseItems, defFileName, context]
		let CollectProductions[tree, first, context] ← 
			<FakeCopyInt[first], FakeCopyContextForConditional[context]>
		let CollectRightSideSymbols[tree, context] ←
				FakeCopyContextForConditional[context]
		let DefFileCode[tree, context, usage] ← DefFileCode[BaseItems, context, usage]
		let ImplFileCode[tree, context, usage] ← ImplFileCode[BaseItems, context, usage]
		let LinkFileCode[tree, context, usage] ← <BuildEmptyCode[], FakeUsageCopy[usage]>
		let SyntaxFileCodes[tree, context] ← <BuildEmptyCode[], BuildEmptyCode[]>
		let TestLinkPresent[tree] ← EqualRopes["True", "False"];
		

for ModuleItem.abGramItems: AbstractProduction[AbGramItems]
		let CollectModuleValueTypes[tree, defFileName, context] ←
				CollectModuleValueTypes[AbGramItems, defFileName, context]
		let CollectModuleFunctionTypes[tree, defFileName, context] ←
				CollectModuleFunctionTypes[AbGramItems, defFileName, context]
		let CollectProductions[tree, first, context] ← 
			<FakeCopyInt[first], FakeCopyContextForConditional[context]>
		let CollectRightSideSymbols[tree, context] ←
				FakeCopyContextForConditional[context]
		let DefFileCode[tree, context, usage] ← DefFileCode[AbGramItems, context, usage]
		let ImplFileCode[tree, context, usage] ← ImplFileCode[AbGramItems, context, usage]
		let LinkFileCode[tree, context, usage] ← <BuildEmptyCode[], FakeUsageCopy[usage]>
		let SyntaxFileCodes[tree, context] ← <BuildEmptyCode[], BuildEmptyCode[]>
		let TestLinkPresent[tree] ← EqualRopes["True", "False"];
		

for ModuleItem.cGramItems: AbstractProduction[CGramItems]
		let CollectModuleValueTypes[tree, defFileName, context] ← 
			FakeCopyContextForConditional[context]
		let CollectModuleFunctionTypes[tree, defFileName, context] ← 
			FakeCopyContextForConditional[context]
		let CollectProductions[tree, first, context] ← CollectProductions[CGramItems, first, context]
		let CollectRightSideSymbols[tree, context] ← CollectRightSideSymbols[CGramItems, context]
		let DefFileCode[tree, context, usage] ← <BuildEmptyCode[], FakeUsageCopy[usage]>
		let ImplFileCode[tree, context, usage] ← ImplFileCode[CGramItems, context, usage]
		let LinkFileCode[tree, context, usage] ← LinkFileCode[CGramItems, context, usage]
		let SyntaxFileCodes[tree, context] ← SyntaxFileCodes[CGramItems, context]
		let TestLinkPresent[tree] ← TestLinkPresent[CGramItems];

for ModuleItem.genTkn: AbstractProduction[Identifier, Rope.tknName]
	let CollectModuleValueTypes[tree, file, context] ←
			RecordGenericToken[context, name, file, codeName]
		where name ← BuildName[Identifier]
		where codeName ← RopeCode1["%gNode", IdFill[Identifier]]

	let CollectModuleFunctionTypes[tree, file, context] ← 
			FakeCopyContextForConditional[context]
	
	let CollectProductions[tree, first, context] ← 
			<FakeCopyInt[first], FakeCopyContextForConditional[context]>
	
	let CollectRightSideSymbols[tree, context] ←
			FakeCopyContextForConditional[context]
	
	let DefFileCode[tree, context, usage] ← <code, use2>
		where code ← ConcatCode3[
						headerCode,
						nodeCode,
						nodeBodyCode]
		
		where use2 ← RecordTypeUse[use1, LookUpType[context, BuildRopeName["INT"]]]
		where use1 ← RecordTypeUse[usage, LookUpType[context, BuildRopeName["ROPE"]]]
		where headerCode ← RopeCode1["\N--%g\N\N", IdFill[Identifier]]
		where nodeCode ← RopeCode2["%g: TYPE = REF %gBody;\N",
									CodeFill[codeName], CodeFill[codeName]]
		where nodeBodyCode ← ConcatCode3[
					RopeCode1["%gBody: TYPE = RECORD[\N", CodeFill[codeName]],
					RopeCode["\Ttext: Rope.ROPE,\N"],
					RopeCode["\Tposition, length: INT];\N\N"]]
			
		where codeName ← GetTypeCodeName[LookUpType[context, name]] 
		where name ← BuildName[Identifier]		

	let ImplFileCode[tree, context, usage] ← <code, use5>
		where code ← ConcatCode2[
						headerCode,
						buildImplCode]
		where use5 ← RecordDefFileUse[use4, "Rope", Use.import]
		where use4 ← RecordTypeUse[use3, LookUpType[context, BuildRopeName["INT"]]]
		where use3 ← RecordTypeUse[use2, LookUpType[context, BuildRopeName["ROPE"]]]
		where headerCode ← RopeCode1["\N--%g\N\N", IdFill[Identifier]]
		
		where buildImplCode ← ConcatCode2[
				RopeCode2["Build%g: PUBLIC PROC[text: Rope.ROPE, firstCharPosition: INT] RETURNS[%g] =\N",
						CodeFill[nameCode],
						CodeFill[nameCode]], 
				RopeCode1["\T{RETURN[NEW[%gBody←[text, firstCharPosition, Length[text]]]]};\N\N",
						CodeFill[nameCode]]]
		
		where nameCode ← GetTypeCodeName[LookUpType[context, name]] 
		
		where use2 ← RecordGenericTokenUse[use1, name, Use.ref, context]
		where use1 ← RecordGenericTokenUse[usage, name, Use.export, context]
		where name ← BuildName[Identifier]
	
	let LinkFileCode[tree, context, usage] ← <code, FakeUsageCopy[usage]>
		where code ← RopeCode2["tknLinkArray[%g] ← Build%gNode;\N", CodeFill[RopeCode[RopeFromRopeNode[Rope.tknName]]], IdFill[Identifier]]
	
	let SyntaxFileCodes[tree, context] ← <tkn, prod>
		where tkn ← RopeCode2["%g: GenericToken = \"%g\" ;\N",
				IdFill[Identifier],
				CodeFill[RopeCode[RopeFromRopeNode[Rope.tknName]]]]
		where prod ← BuildEmptyCode[]
	
	let TestLinkPresent[tree] ← EqualRopes["True", "True"];

		


for CedarItems.cedarTypes: AbstractProduction[IdList]
	let CollectModuleValueTypes[tree, defFileName, context] ←
	 		RecordCedarTypes[IdList, context]
	let CollectModuleFunctionTypes[tree, defFileName, context] ← 
			FakeCopyContextForConditional[context];

for CedarItems.cedarTypesFrom: AbstractProduction[IdList, Identifier]
	let CollectModuleValueTypes[tree, defFileName, context] ←
	 		RecordCedarTypesFrom[IdList, file, context]
	 	where file ← RopeFromIdentifierNode[Identifier]
	let CollectModuleFunctionTypes[tree, defFileName, context] ← 
			FakeCopyContextForConditional[context];
		
for CedarItems.cedarEnumTypeFrom:
			AbstractProduction[Identifier.typeName, IdList.values, Identifier.fileName]
	let CollectModuleValueTypes[tree, defFileName, context] ← 
			RecordEnumeratedBaseType[context, name, file, names, codeName]
		where name ← BuildName[Identifier.typeName]
		where names ← FormNameList[IdList.values]
		where file ← RopeFromIdentifierNode[Identifier.fileName]
		where codeName ← RopeCode1["%g", IdFill[Identifier.typeName]]
	let CollectModuleFunctionTypes[tree, defFileName, context] ← 
			FakeCopyContextForConditional[context];
		
for CedarItems.cedarFunctionFrom: 
			AbstractProduction[Identifier.funName, ModIdList.args, ModIdList.results, Identifier.fileName, DamageShareAssertions]
	let CollectModuleValueTypes[tree, defFileName, context] ←
			FakeCopyContextForConditional[context]
	let CollectModuleFunctionTypes[tree, defFileName, context] ←
		RecordBaseFunction[context, BuildName[Identifier.funName],
					defFileName, argTypes, resultTypes, fcnDefGraph]
			where argTypes ← FormTypeListEarly[ModIdList.args, context]
			where resultTypes ← FormTypeListEarly[ModIdList.results, context]
			where defFileName ← RopeFromIdentifierNode[Identifier.fileName]
			where fcnDefGraph ←
				FormRecordOfDamageShareAssertion[DamageShareAssertions, fcnDefGraph1]
			where fcnDefGraph1 ← BuildFcnBaseGraph[argNames, resultNames]
			where argNames ← FormNameList[ModIdList.args]
			where resultNames ← FormNameList[ModIdList.results];
		
	
	
for CGramItems.simpleTokens: AbstractProduction[RopeList]
	let CollectProductions[tree, first, context] ←
			<FakeCopyInt[first], FakeCopyContextForConditional[context]>
	let CollectRightSideSymbols[tree, context] ← CollectRightSideSymbols[RopeList, context]
	let DefFileCode[tree, context, usage] ←
			<BuildEmptyCode[], FakeUsageCopy[usage]>
	let ImplFileCode[tree, context, usage] ←
			<BuildEmptyCode[], FakeUsageCopy[usage]>
	let LinkFileCode[tree, context, usage] ←
			<BuildEmptyCode[], FakeUsageCopy[usage]>
	let SyntaxFileCodes[tree, context] ← <tkn, prod>
		where tkn ← RopeCode1["{ %g }: SimpleTokens;\N", CodeFill[simples]]
		where <simples, unused> ← SyntaxFileCodes[RopeList, context]
		where prod ← BuildEmptyCode[]
	let TestLinkPresent[tree] ← EqualRopes["True", "False"];


for CGramItems.nonTerminal: AbstractProduction[Identifier.name, Identifier.builds]
	let CollectProductions[tree, first, context] ←
			<FakeCopyInt[first], FakeCopyContextForConditional[context]>
	let CollectRightSideSymbols[tree, context] ←
			RecordNonTerminalToken[context, ntName, atName]
		where ntName ← BuildName[Identifier.name]
		where atName ← BuildName[Identifier.builds]
	let DefFileCode[tree, context, usage] ←
			<BuildEmptyCode[], FakeUsageCopy[usage]>
	let ImplFileCode[tree, context, usage] ←
			<BuildEmptyCode[], FakeUsageCopy[usage]>
	let LinkFileCode[tree, context, usage] ←
			<BuildEmptyCode[], FakeUsageCopy[usage]>
	let SyntaxFileCodes[tree, context] ← <tkn, prod>
		where tkn ← RopeCode1["%g: NonTerminal;\N", IdFill[Identifier.name]]
		where prod ← BuildEmptyCode[]
	let TestLinkPresent[tree] ← EqualRopes["True", "False"];

for CGramItems.concreteProduction: AbstractProduction[ConcreteProduction]
	let CollectProductions[tree, first, context] ←
			CollectProductions[ConcreteProduction, first, context]
	let CollectRightSideSymbols[tree, context] ← FakeCopyContextForConditional[context]
	let DefFileCode[tree, context, usage] ← <BuildEmptyCode[], FakeUsageCopy[usage]>
	let ImplFileCode[tree, context, usage] ← ImplFileCode[ConcreteProduction, context, usage]
	let LinkFileCode[tree, context, usage] ← LinkFileCode[ConcreteProduction, context, usage]
	let SyntaxFileCodes[tree, context] ← SyntaxFileCodes[ConcreteProduction, context]
	let TestLinkPresent[tree] ← EqualRopes["True", "True"]
	



End.