Multiple changes:

- Add the book and a few first pages
- Add the code generator
This commit is contained in:
2022-04-17 19:48:45 +02:00
parent 0469c6dbdf
commit 199758d97d
47 changed files with 5143 additions and 41 deletions

View File

@@ -4,6 +4,12 @@ Class {
#category : #'Carp-AST'
}
{ #category : #accessing }
CarpExpressionNode class >> itself [
<gtExample>
^ self
]
{ #category : #generated }
CarpExpressionNode >> acceptVisitor: anExpressionVisitor [

View File

@@ -1,34 +0,0 @@
Class {
#name : #CarpModuleOrTypeNode,
#superclass : #CarpExpressionNode,
#instVars : [
'value'
],
#category : #'Carp-AST'
}
{ #category : #generated }
CarpModuleOrTypeNode >> acceptVisitor: anExpressionVisitor [
^ anExpressionVisitor visitModuleOrType: self
]
{ #category : #generated }
CarpModuleOrTypeNode >> otherVariables [
^ #( #value )
]
{ #category : #generated }
CarpModuleOrTypeNode >> value [
^ value
]
{ #category : #generated }
CarpModuleOrTypeNode >> value: anObject [
self setParent: self value to: nil.
value := anObject.
self setParent: self value to: self
]

View File

@@ -0,0 +1,53 @@
Class {
#name : #CarpModuleOrTypeNode,
#superclass : #CarpExpressionNode,
#instVars : [
'module',
'value'
],
#category : #'Carp-Parser'
}
{ #category : #generated }
CarpModuleOrTypeNode >> acceptVisitor: anExpressionVisitor [
^ anExpressionVisitor visitModuleOrType: self
]
{ #category : #generated }
CarpModuleOrTypeNode >> module [
^ module
]
{ #category : #generated }
CarpModuleOrTypeNode >> module: aSmaCCToken [
module := aSmaCCToken
]
{ #category : #generated }
CarpModuleOrTypeNode >> nodeVariables [
^ #( #value )
]
{ #category : #generated }
CarpModuleOrTypeNode >> tokenVariables [
^ #( #module )
]
{ #category : #generated }
CarpModuleOrTypeNode >> value [
^ value
]
{ #category : #generated }
CarpModuleOrTypeNode >> value: aCarpExpressionNode [
self value notNil ifTrue: [ self value parent: nil ].
value := aCarpExpressionNode.
self value notNil ifTrue: [ self value parent: self ]
]

View File

@@ -6,7 +6,7 @@ Class {
{ #category : #'generated-accessing' }
CarpParser class >> cacheId [
^'2022-04-16T18:21:43.028213+02:00'
^'2022-04-17T17:01:25.369965+02:00'
]
{ #category : #generated }
@@ -141,8 +141,8 @@ Variable
: <variable> 'value' {{}}
;
ModuleOrType
: <module> <dot> Symbol 'value' {{}}
| <module> 'value' {{}}
: <module> 'module' <dot> Symbol 'value' {{}}
| <module> 'module' {{}}
;
List
: <open_paren> 'leftParen' Expressions <close_paren> 'rightParen' {{}}
@@ -158,7 +158,7 @@ CarpParser class >> reduceTable [
#(24 0 #reduceActionForExpressions1: 1289217 false)
#(23 1 #reduceActionForStart1: 1258497 false)
#(32 0 #reduceActionForExpressions1: 1803265 false)
#(43 1 #reduceActionForString1: 2436097 false)
#(43 1 #reduceActionForString1: 2446337 false)
#(40 1 #reduceActionForVariable1: 2233345 false)
#(41 1 #reduceActionForModuleOrType2: 2273282 false)
#(36 1 #reduceActionForNumber1: 2013185 false)
@@ -189,7 +189,7 @@ CarpParser class >> reduceTable [
#(38 2 #reduceActionForQuote1: 2150401 false)
#(31 3 #reduceActionForMap1: 1744897 false)
#(32 2 #reduceActionForExpressions2: 1803266 false)
#(42 3 #reduceActionForList1: 2354177 false)
#(42 3 #reduceActionForList1: 2364417 false)
#(41 3 #reduceActionForModuleOrType1: 2273281 false)
#(37 3 #reduceActionForArray1: 2059265 false)
#(33 2 #reduceActionForMapPair1: 1846273 false)
@@ -379,6 +379,7 @@ CarpParser >> reduceActionForModuleOrType1: nodes [
| result |
result := CarpModuleOrTypeNode new.
result module: (nodes at: 1).
result value: (nodes at: 3).
^ result
]
@@ -388,7 +389,7 @@ CarpParser >> reduceActionForModuleOrType2: nodes [
| result |
result := CarpModuleOrTypeNode new.
result value: (nodes at: 1).
result module: (nodes at: 1).
^ result
]

View File

@@ -0,0 +1,11 @@
Extension { #name : #ByteString }
{ #category : #'*Carp' }
ByteString >> asCarpModule [
^ CarpModule named: self
]
{ #category : #'*Carp' }
ByteString >> asCarpSymbol [
^ CarpSymbol named: self
]

View File

@@ -0,0 +1,23 @@
Class {
#name : #CarpBinding,
#superclass : #CarpExpression,
#instVars : [
'name'
],
#category : #'Carp-IDE'
}
{ #category : #accessing }
CarpBinding >> bindingName [
^ self subclassResponsibility
]
{ #category : #accessing }
CarpBinding >> name [
^ name
]
{ #category : #accessing }
CarpBinding >> name: aString [
name := aString
]

View File

@@ -0,0 +1,43 @@
Class {
#name : #CarpCall,
#superclass : #CarpExpression,
#instVars : [
'function',
'arguments'
],
#category : #'Carp-IDE'
}
{ #category : #'instance creation' }
CarpCall class >> function: aSymbol arguments: aCollection [
^ self new name: aSymbol; arguments: aCollection
]
{ #category : #accessing }
CarpCall >> addArgument: anArgument [
arguments add: anArgument
]
{ #category : #accessing }
CarpCall >> arguments: aCollection [
arguments := aCollection
]
{ #category : #initialization }
CarpCall >> initialize [
arguments := OrderedCollection new
]
{ #category : #accessing }
CarpCall >> name: aSymbol [
function := aSymbol
]
{ #category : #accessing }
CarpCall >> toCarp [
^ String streamContents: [ :aStream |
aStream << '(' << function toCarp.
arguments do: [ :anArgument | aStream << ' ' << anArgument toCarp ].
aStream << ')' ]
]

View File

@@ -0,0 +1,5 @@
Class {
#name : #CarpDouble,
#superclass : #CarpNumber,
#category : #'Carp-IDE'
}

View File

@@ -0,0 +1,10 @@
Class {
#name : #CarpDynamicFunction,
#superclass : #CarpNamedFunction,
#category : #'Carp-IDE'
}
{ #category : #accessing }
CarpDynamicFunction >> bindingName [
^ 'defndynamic'
]

View File

@@ -0,0 +1,10 @@
Class {
#name : #CarpDynamicVariable,
#superclass : #CarpVariable,
#category : #'Carp-IDE'
}
{ #category : #accessing }
CarpDynamicVariable >> bindingName [
^ 'defdynamic'
]

View File

@@ -0,0 +1,10 @@
Class {
#name : #CarpExpression,
#superclass : #Object,
#category : #'Carp-IDE'
}
{ #category : #converting }
CarpExpression >> toCarp [
^ self subclassResponsibility
]

View File

@@ -5,7 +5,7 @@ Class {
'fileName',
'contents'
],
#category : #'Carp-Parser'
#category : #'Carp-IDE'
}
{ #category : #'instance creation' }

View File

@@ -0,0 +1,10 @@
Class {
#name : #CarpFloat,
#superclass : #CarpNumber,
#category : #'Carp-IDE'
}
{ #category : #accessing }
CarpFloat >> suffix [
^ 'f'
]

View File

@@ -0,0 +1,10 @@
Class {
#name : #CarpInteger,
#superclass : #CarpNumber,
#category : #'Carp-IDE'
}
{ #category : #accessing }
CarpInteger >> suffix [
^ ''
]

View File

@@ -0,0 +1,5 @@
Class {
#name : #CarpLiteral,
#superclass : #CarpExpression,
#category : #'Carp-IDE'
}

View File

@@ -0,0 +1,10 @@
Class {
#name : #CarpLong,
#superclass : #CarpNumber,
#category : #'Carp-IDE'
}
{ #category : #accessing }
CarpLong >> suffix [
^ 'l'
]

View File

@@ -0,0 +1,10 @@
Class {
#name : #CarpMacro,
#superclass : #CarpNamedFunction,
#category : #'Carp-IDE'
}
{ #category : #accessing }
CarpMacro >> bindingName [
^ 'defmacro'
]

View File

@@ -0,0 +1,55 @@
Class {
#name : #CarpModule,
#superclass : #CarpExpression,
#instVars : [
'uses',
'expressions',
'name'
],
#category : #'Carp-IDE'
}
{ #category : #'instance creation' }
CarpModule class >> named: aString [
^ self new name: aString
]
{ #category : #accessing }
CarpModule >> addExpression: anExpression [
expressions add: anExpression
]
{ #category : #accessing }
CarpModule >> addUse: aString [
uses add: aString
]
{ #category : #initialization }
CarpModule >> initialize [
uses := Set new.
expressions := OrderedCollection new.
]
{ #category : #converting }
CarpModule >> name [
^ name
]
{ #category : #accessing }
CarpModule >> name: aString [
name := aString
]
{ #category : #converting }
CarpModule >> toCarp [
^ String streamContents: [ :aStream |
aStream << '(defmodule ' << self name << ' '
<< (uses ifEmpty: [ '' ] ifNotEmpty: [
Character lf , Character tab , '(use-all '
, (' ' join: uses) , ')' ]).
expressions do: [ :expression |
aStream << Character lf << Character tab
<< expression toCarp ].
aStream << ')' ]
]

View File

@@ -0,0 +1,53 @@
Class {
#name : #CarpNamedFunction,
#superclass : #CarpBinding,
#instVars : [
'arguments',
'body'
],
#category : #'Carp-IDE'
}
{ #category : #'instance creation' }
CarpNamedFunction class >> named: aString withArguments: aCollection andBody: anExpression [
^ self new
name: aString;
arguments: aCollection;
body: anExpression
]
{ #category : #accessing }
CarpNamedFunction >> addArgument: anArgument [
arguments add: anArgument
]
{ #category : #accessing }
CarpNamedFunction >> arguments: aCollection [
arguments := aCollection
]
{ #category : #accessing }
CarpNamedFunction >> body [
^ body
]
{ #category : #accessing }
CarpNamedFunction >> body: anExpression [
body := anExpression
]
{ #category : #initialization }
CarpNamedFunction >> initialize [
arguments := OrderedCollection new
]
{ #category : #converting }
CarpNamedFunction >> toCarp [
^ String streamContents: [ :aStream |
aStream << '(' << self bindingName << ' ' << self name toCarp
<< ' ['.
arguments do: [ :anArgument | aStream << anArgument toCarp << ' ' ].
aStream << '] ' << self body toCarp << ')' ]
]

View File

@@ -0,0 +1,28 @@
Class {
#name : #CarpNumber,
#superclass : #CarpLiteral,
#instVars : [
'number'
],
#category : #'Carp-IDE'
}
{ #category : #'instance creation' }
CarpNumber class >> number: aNumber [
^ self new number: aNumber
]
{ #category : #acccessing }
CarpNumber >> number: aNumber [
number := aNumber
]
{ #category : #accessing }
CarpNumber >> suffix [
^ self subclassResponsibility
]
{ #category : #converting }
CarpNumber >> toCarp [
^ number asString, self suffix
]

View File

@@ -0,0 +1,10 @@
Class {
#name : #CarpStaticFunction,
#superclass : #CarpNamedFunction,
#category : #'Carp-IDE'
}
{ #category : #accessing }
CarpStaticFunction >> bindingName [
^ 'defn'
]

View File

@@ -0,0 +1,10 @@
Class {
#name : #CarpStaticVariable,
#superclass : #CarpVariable,
#category : #'Carp-IDE'
}
{ #category : #accessing }
CarpStaticVariable >> bindingName [
^ 'def'
]

View File

@@ -0,0 +1,23 @@
Class {
#name : #CarpStringExpression,
#superclass : #CarpExpression,
#instVars : [
'contents'
],
#category : #'Carp-IDE'
}
{ #category : #'instance creation' }
CarpStringExpression class >> contents: aString [
^ self new contents: aString
]
{ #category : #'instance creation' }
CarpStringExpression >> contents: aString [
contents := aString
]
{ #category : #accessing }
CarpStringExpression >> toCarp [
^ contents asString
]

View File

@@ -0,0 +1,23 @@
Class {
#name : #CarpSymbol,
#superclass : #CarpLiteral,
#instVars : [
'name'
],
#category : #'Carp-IDE'
}
{ #category : #'instance creation' }
CarpSymbol class >> named: aString [
^ self new name: aString
]
{ #category : #accessing }
CarpSymbol >> name: aString [
name := aString
]
{ #category : #accessing }
CarpSymbol >> toCarp [
^ name
]

View File

@@ -0,0 +1,34 @@
Class {
#name : #CarpVariable,
#superclass : #CarpBinding,
#instVars : [
'binding'
],
#category : #'Carp-IDE'
}
{ #category : #'instance creation' }
CarpVariable class >> named: aString binding: anExpression [
^ self new
name: aString;
binding: anExpression
]
{ #category : #accessing }
CarpVariable >> binding [
^ binding
]
{ #category : #accessing }
CarpVariable >> binding: anExpression [
binding := anExpression
]
{ #category : #converting }
CarpVariable >> toCarp [
^ String streamContents: [ :aStream |
aStream << '(' << self bindingName << ' ' << self name toCarp
<< ' ' << self binding toCarp << ')' ]
]

View File

@@ -0,0 +1,11 @@
Extension { #name : #Float }
{ #category : #'*Carp' }
Float >> asCarpDouble [
^ CarpDouble number: self
]
{ #category : #'*Carp' }
Float >> asCarpFloat [
^ CarpFloat number: self
]

View File

@@ -0,0 +1,11 @@
Extension { #name : #Integer }
{ #category : #'*Carp' }
Integer >> asCarpInteger [
^ CarpInteger number: self
]
{ #category : #'*Carp' }
Integer >> asCarpLong [
^ CarpLong number: self
]