SystemWindow subclass: #LLWindow
 instanceVariableNames: 'fixedExtent' classVariableNames: '' poolDictionaries: '' category: 'LearnSqueak-Language'!

Object subclass: #LLLexer
 instanceVariableNames: 'source cursor tokens' classVariableNames: '' poolDictionaries: '' category: 'LearnSqueak-Language'!

Object subclass: #LLParser
 instanceVariableNames: 'tokens cursor depth' classVariableNames: '' poolDictionaries: '' category: 'LearnSqueak-Language'!

Object subclass: #LLInterpreter
 instanceVariableNames: 'variables' classVariableNames: '' poolDictionaries: '' category: 'LearnSqueak-Language'!

Object subclass: #LLTreePrinter
 instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'LearnSqueak-Language'!

Morph subclass: #LLLanguageApp
 instanceVariableNames: 'interpreter editor tokenPane treePane resultLabel varsLabel statusLabel source tree result status history window' classVariableNames: '' poolDictionaries: '' category: 'LearnSqueak-Language'!

!LLWindow methodsFor: 'tutorial'!
fixedExtent: point
 fixedExtent := point. super extent: point
! !


!LLWindow methodsFor: 'tutorial'!
extent: point
 super extent: (fixedExtent ifNil: [point])
! !


!LLLexer methodsFor: 'tutorial'!
scan: text
 text size > 1000 ifTrue: [self error: 'Limit: 1000 source characters'].
 source := text asString. cursor := 1. tokens := OrderedCollection new.
 [cursor <= source size] whileTrue: [self scanOne].
 tokens addLast: {#end. ''. cursor}. ^ tokens asArray
! !


!LLLexer methodsFor: 'tutorial'!
isDigit: char
 ^ char >= $0 and: [char <= $9]
! !


!LLLexer methodsFor: 'tutorial'!
isLetter: char
 ^ (char >= $a and: [char <= $z]) or: [char >= $A and: [char <= $Z]]
! !


!LLLexer methodsFor: 'tutorial'!
add: kind value: value at: start
 tokens size >= 200 ifTrue: [self error: 'Limit: 200 tokens'].
 tokens addLast: {kind. value. start}
! !


!LLLexer methodsFor: 'tutorial'!
scanOne
 | char start value |
 char := source at: cursor.
 char isSeparator ifTrue: [cursor := cursor + 1. ^ self].
 start := cursor.
 (self isDigit: char) ifTrue: [
  [cursor <= source size and: [self isDigit: (source at: cursor)]] whileTrue: [cursor := cursor + 1].
  cursor - start > 9 ifTrue: [self error: 'Integer needs at most 9 digits at column ', start printString].
  value := (source copyFrom: start to: cursor - 1) asInteger.
  ^ self add: #number value: value at: start].
 (self isLetter: char) ifTrue: [
  [cursor <= source size and: [(self isLetter: (source at: cursor)) or: [self isDigit: (source at: cursor)]]] whileTrue: [cursor := cursor + 1].
  cursor - start > 24 ifTrue: [self error: 'Name needs at most 24 letters/digits at column ', start printString].
  ^ self add: #name value: (source copyFrom: start to: cursor - 1) at: start].
 ('+-*/()=' includes: char) ifTrue: [cursor := cursor + 1. ^ self add: char asString asSymbol value: char asString at: start].
 self error: 'Unexpected character at column ', cursor printString
! !


!LLLanguageApp methodsFor: 'tutorial'!
initialize
 super initialize. self color: (Color fromString: '#EEF2F8'); extent: 830@590.
 source := '2 + 3 * 4'. status := 'Type a tiny expression, then inspect its tokens.'. history := OrderedCollection new.
 self initializeInterpreter.
 self label: 'Tiny Language Lab' at: 24@13 size: 27.
 self label: ('Checkpoint ', self stage printString, ' / 9: ', self stageTitle) at: 25@53 size: 14.
 self label: 'INPUT  /  integer arithmetic, with deliberate syntax' at: 25@88 size: 13.
 editor := PluggableTextMorph on: self text: #source accept: #acceptSource:.
 editor acceptOnCR: false; plainTextOnly: true; extent: 779@53; position: self position + (25@113). self addMorph: editor.
 self buildToolbar.
 self label: 'TOKENS  /  kind, value, source column' at: 25@220 size: 13.
 self label: 'SYNTAX TREE  /  the structure we execute' at: 417@220 size: 13.
 tokenPane := self paneAt: 25@245 extent: 376@204.
 treePane := self paneAt: 417@245 extent: 388@204.
 resultLabel := self label: 'Result: waiting' at: 25@463 size: 20.
 varsLabel := self label: 'Variables: none' at: 25@502 size: 13.
 statusLabel := self label: status at: 25@541 size: 13
! !


!LLLanguageApp methodsFor: 'tutorial'!
initializeInterpreter
 interpreter := nil
! !


!LLLanguageApp methodsFor: 'tutorial'!
label: text at: point size: size
 | label | label := StringMorph contents: text font: (TextStyle default fontOfSize: size).
 label color: (Color fromString: '#183A58'); position: self position + point. self addMorph: label. ^ label
! !


!LLLanguageApp methodsFor: 'tutorial'!
paneAt: point extent: size
 | pane | pane := PluggableTextMorph on: nil text: nil accept: nil. pane setText: ''.
 pane extent: size; position: self position + point; color: Color white. self addMorph: pane. ^ pane
! !


!LLLanguageApp methodsFor: 'tutorial'!
button: label at: x action: selector
 | button | button := SimpleButtonMorph new label: label; target: self; actionSelector: selector; color: Color white; position: self position + (x@183). self addMorph: button
! !


!LLLanguageApp methodsFor: 'tutorial'!
buttonNamed: text
 ^ self submorphs detect: [:morph | (morph isKindOf: SimpleButtonMorph) and: [morph label = text]]
! !


!LLLanguageApp methodsFor: 'tutorial'!
source
 ^ source
! !


!LLLanguageApp methodsFor: 'tutorial'!
source: text
 source := text asString. editor setText: source asText
! !


!LLLanguageApp methodsFor: 'tutorial'!
acceptSource: text
 source := text asString. ^ true
! !


!LLLanguageApp methodsFor: 'tutorial'!
editor
 ^ editor
! !


!LLLanguageApp methodsFor: 'tutorial'!
status
 ^ status
! !


!LLLanguageApp methodsFor: 'tutorial'!
result
 ^ result
! !


!LLLanguageApp methodsFor: 'tutorial'!
tree
 ^ tree
! !


!LLLanguageApp methodsFor: 'tutorial'!
interpreter
 ^ interpreter
! !


!LLLanguageApp methodsFor: 'tutorial'!
history
 ^ history copy
! !


!LLLanguageApp methodsFor: 'tutorial'!
tokenText: tokens
 ^ String streamContents: [:out | tokens do: [:token | out nextPutAll: token first asString; nextPutAll: '   '; nextPutAll: token second printString; nextPutAll: '   @'; nextPutAll: token third printString; cr]]
! !


!LLLanguageApp methodsFor: 'tutorial'!
showTokens
 | tokens | source := editor text asString.
 [tokens := LLLexer new scan: source. tokenPane setText: (self tokenText: tokens). status := (tokens size - 1) printString, ' tokens plus one end marker.']
 on: Error do: [:error | tokenPane setText: ''. status := error messageText]. self refresh
! !


!LLLanguageApp methodsFor: 'tutorial'!
refresh
 resultLabel contents: (result ifNil: ['Result: waiting / rejected'] ifNotNil: ['Result: ', result printString]).
 statusLabel contents: (status truncateTo: 102).
 interpreter ifNotNil: [varsLabel contents: ('Variables: ', (String streamContents: [:out | interpreter variables keys asSortedCollection do: [:name | out nextPutAll: name; nextPutAll: ' = '; nextPutAll: (interpreter variables at: name) printString; nextPutAll: '   ']]) truncateTo: 102)]. ^ self
! !


!LLLanguageApp methodsFor: 'tutorial'!
open
 window := LLWindow new model: self; setLabel: 'Tiny Language Lab | Squeak 6.1'.
 window addMorph: self fullFrame: (LayoutFrame fractions: (0@0 corner: 1@1)). window fixedExtent: 840@623. window openInWorld. ^ self
! !


!LLLanguageApp methodsFor: 'tutorial'!
window
 ^ window
! !


!LLLanguageApp class methodsFor: 'tutorial'!
open
 ^ self new open
! !


!LLLanguageApp methodsFor: 'tutorial'!
stage
 ^ 1
! !


!LLLanguageApp methodsFor: 'tutorial'!
stageTitle
 ^ 'Scan a source string'
! !


!LLLanguageApp methodsFor: 'tutorial'!
buildToolbar
 self button: 'Tokens' at: 25 action: #showTokens.
! !

