compiler/PPCTokenizingCodeGenerator.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Thu, 30 Apr 2015 23:43:14 +0200
changeset 438 20598d7ce9fa
child 452 9f4558b3be66
permissions -rw-r--r--
Updated to PetitCompiler-JanKurs.100, PetitCompiler-Tests-JanKurs.44 and PetitCompiler-Benchmarks-JanKurs.4 Name: PetitCompiler-JanKurs.100 Author: JanKurs Time: 30-04-2015, 10:48:52.165 AM UUID: 80196870-5921-46d9-ac20-a43bf5c2f3c2 Name: PetitCompiler-Tests-JanKurs.44 Author: JanKurs Time: 30-04-2015, 10:49:22.489 AM UUID: 348c02e8-18ce-48f6-885d-fcff4516a298 Name: PetitCompiler-Benchmarks-JanKurs.4 Author: JanKurs Time: 30-04-2015, 10:58:44.890 AM UUID: 18cadb42-f9ef-45fb-82e9-8469ade56c8b

"{ Package: 'stx:goodies/petitparser/compiler' }"

"{ NameSpace: Smalltalk }"

PPCCodeGenerator subclass:#PPCTokenizingCodeGenerator
	instanceVariableNames:''
	classVariableNames:''
	poolDictionaries:''
	category:'PetitCompiler-Visitors'
!

!PPCTokenizingCodeGenerator methodsFor:'visiting'!

visitLLChoiceNode: node
	| dictionary currentTokenVar |
	dictionary := IdentityDictionary new.
	
	node children do: [ :child |
		| firstSet |
		firstSet := child firstSetSuchThat: [ :e | e isKindOf: PPCTokenNode ].
		self assert: firstSet size = 1.
		dictionary at: child 
			put: firstSet anyOne.
			
	].
	"Tokens are unique"
	self assert: dictionary values asSet size = node children size.
	
	compiler addConstant: (dictionary values collect: [ :e | compiler idFor: e ])
				as: #tokenMethods.
	
	currentTokenVar := compiler allocateTemporaryVariableNamed: 'currentToken'.
	compiler codeAssign: 'self currentTokenType.' to: currentTokenVar.
	node children do: [ :child |
		| tokenMethodName |
		tokenMethodName := compiler idFor: (dictionary at: child).
		compiler add: currentTokenVar , ' = ',  tokenMethodName storeString.
		compiler add: 'ifTrue: ['.
		compiler codeStoreValueOf: [ self visit: child ] intoVariable: self retvalVar.
		compiler codeReturn: self retvalVar.
		compiler add: '].'
	].

	compiler codeError: 'no choice found'.
!

visitTokenConsumeNode: node
	compiler codeReturn: 'self consume: ', (compiler idFor: node child) storeString, '.'
!

visitTokenNode: node
	| tokenType |
	self assert: node isMarkedForInline.

	super visitTokenNode: node.
	
	tokenType := compiler idFor: node.

	compiler codeAssign: tokenType storeString, '.' to: 'currentTokenType'.
	compiler codeAssign: self retvalVar, '.' to: 'currentTokenValue'.
! !