Skip to content

Commit 0df4087

Browse files
authored
Refactor proper ast #18 (#20)
* refactor: migration to proper AST * refactor: Parse function expressions * extern docs * improved docs * relative errors * centralize function calls * nicer stack traces * bump dependencies * remove old interpreter * improved error handling
1 parent 4e1e5a8 commit 0df4087

File tree

168 files changed

+4782
-2795
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

168 files changed

+4782
-2795
lines changed

.vscode/launch.json

+44-48
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,45 @@
11
{
2-
// Use IntelliSense to learn about possible attributes.
3-
// Hover to view descriptions of existing attributes.
4-
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5-
"version": "0.2.0",
6-
"configurations": [
7-
{
8-
"name": "Write docs",
9-
"type": "go",
10-
"request": "launch",
11-
"mode": "auto",
12-
"program": "${workspaceFolder}/cmd/lithia/main.go",
13-
"args": [
14-
"${workspaceFolder}/stdlib/stdlib-write-docs.lithia"
15-
],
16-
"cwd": "${workspaceFolder}",
17-
"env": {
18-
"LITHIA_STDLIB": "${workspaceFolder}/stdlib"
19-
}
20-
},
21-
{
22-
"name": "Test example stdlib",
23-
"type": "go",
24-
"request": "launch",
25-
"mode": "auto",
26-
"program": "${workspaceFolder}/cmd/lithia/main.go",
27-
"args": [
28-
"${workspaceFolder}/stdlib/stdlib-tests.lithia"
29-
],
30-
"env": {
31-
"LITHIA_TESTS": "1",
32-
"LITHIA_STDLIB": "${workspaceFolder}/stdlib"
33-
}
34-
},
35-
{
36-
"name": "Launch lithia",
37-
"type": "go",
38-
"request": "launch",
39-
"mode": "auto",
40-
"program": "${workspaceFolder}/cmd/lithia/main.go",
41-
"args": [
42-
"${workspaceFolder}/examples/syntax.lithia"
43-
],
44-
"env": {
45-
"LITHIA_STDLIB": "${workspaceFolder}/stdlib"
46-
}
47-
},
48-
]
49-
}
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "Write docs",
9+
"type": "go",
10+
"request": "launch",
11+
"mode": "auto",
12+
"program": "${workspaceFolder}/cmd/lithia/main.go",
13+
"args": ["${workspaceFolder}/stdlib/stdlib-write-docs.lithia"],
14+
"cwd": "${workspaceFolder}",
15+
"env": {
16+
"LITHIA_STDLIB": "${workspaceFolder}/stdlib"
17+
}
18+
},
19+
{
20+
"name": "Test example stdlib",
21+
"type": "go",
22+
"request": "launch",
23+
"mode": "auto",
24+
"program": "${workspaceFolder}/cmd/lithia/main.go",
25+
"args": ["${workspaceFolder}/stdlib/stdlib-tests.lithia"],
26+
"cwd": "${workspaceFolder}",
27+
"env": {
28+
"LITHIA_TESTS": "1",
29+
"LITHIA_STDLIB": "${workspaceFolder}/stdlib"
30+
}
31+
},
32+
{
33+
"name": "Launch lithia",
34+
"type": "go",
35+
"request": "launch",
36+
"mode": "auto",
37+
"program": "${workspaceFolder}/cmd/lithia/main.go",
38+
"args": ["${workspaceFolder}/examples/fib-type.lithia"],
39+
"cwd": "${workspaceFolder}",
40+
"env": {
41+
"LITHIA_STDLIB": "${workspaceFolder}/stdlib"
42+
}
43+
}
44+
]
45+
}

CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# Changelog
22

3+
## unreleased
4+
5+
- stdlib: added `arity` to `prelude.Function`
6+
- compiler: massive performance improvements
7+
- compiler: large refactoring
8+
- fix: rare equality bugs
9+
310
## v0.0.9
411

512
- fix: `==` didn't work for independent values

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,9 @@ func greet { person =>
166166

167167
in Lithia are different than you might know them from other languages.
168168
Other languages define enums as a list of constant values. A few allow associated values for each named case.
169-
A Lithia *enum* is an enumeration of types.
169+
A Lithia _enum_ is an enumeration of types.
170170

171-
There is syntactic sugar for value enumerations, to directly declare a case and the associated *enum* or *data* type.
171+
There is syntactic sugar for value enumerations, to directly declare a case and the associated _enum_ or _data_ type.
172172

173173
```
174174
enum JuristicPerson {
@@ -407,7 +407,7 @@ Most languages allow type casts and checks. Lithia does only support the type sw
407407

408408
These checks are unstructured and therefore tempt to be used in the wrong places. Though type checks should be used sparingly. Lithia prefers to move required decisions to the edge of the code. Witnesses should implement decisions for the provided data and desired behavior.
409409

410-
If there is one type to focus on, the tooling and the developer can understand all cases much easier and faster.
410+
If there is one type to focus on, the developer and the tooling can understand all cases much easier and faster.
411411

412412
## License
413413

ast/context-file.go

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package ast
2+
3+
type SourceFile struct {
4+
Path string
5+
Imports []ModuleName
6+
Declarations []Decl
7+
Statements []Expr
8+
9+
*Source
10+
}
11+
12+
func MakeSourceFile(
13+
Path string,
14+
Source *Source,
15+
) *SourceFile {
16+
return &SourceFile{
17+
Path: Path,
18+
Source: Source,
19+
Imports: make([]ModuleName, 0),
20+
Declarations: make([]Decl, 0),
21+
Statements: make([]Expr, 0),
22+
}
23+
}
24+
25+
func (sf *SourceFile) AddDecl(decl Decl) {
26+
if decl == nil {
27+
return
28+
}
29+
if importDecl, ok := decl.(DeclImport); ok {
30+
sf.Imports = append(sf.Imports, importDecl.ModuleName)
31+
}
32+
sf.Declarations = append(sf.Declarations, decl)
33+
}
34+
35+
func (sf *SourceFile) AddExpr(expr Expr) {
36+
if expr == nil {
37+
return
38+
}
39+
sf.Statements = append(sf.Statements, expr)
40+
}

ast/context-module.go

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package ast
2+
3+
type ModuleName string
4+
5+
type ContextModule struct {
6+
Name ModuleName
7+
8+
Files []*SourceFile
9+
}
10+
11+
func MakeContextModule(name ModuleName) *ContextModule {
12+
return &ContextModule{
13+
Name: name,
14+
Files: []*SourceFile{},
15+
}
16+
}
17+
18+
func (m *ContextModule) AddSourceFile(sourceFile *SourceFile) {
19+
m.Files = append(m.Files, sourceFile)
20+
}

ast/decl-constant.go

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package ast
2+
3+
var _ Decl = DeclConstant{}
4+
5+
type DeclConstant struct {
6+
Name Identifier
7+
Value Expr
8+
9+
Docs *Docs
10+
MetaInfo *MetaDecl
11+
}
12+
13+
func (e DeclConstant) DeclName() Identifier {
14+
return e.Name
15+
}
16+
17+
func (e DeclConstant) Meta() *MetaDecl {
18+
return e.MetaInfo
19+
}
20+
21+
func (e DeclConstant) IsExportedDecl() bool {
22+
return true
23+
}
24+
25+
func MakeDeclConstant(name Identifier, value Expr, source *Source) *DeclConstant {
26+
return &DeclConstant{
27+
Name: name,
28+
Value: value,
29+
MetaInfo: &MetaDecl{source},
30+
}
31+
}

ast/decl-data.go

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package ast
2+
3+
var _ Decl = DeclData{}
4+
5+
type DeclData struct {
6+
Name Identifier
7+
Fields []DeclField
8+
9+
Docs *Docs
10+
MetaInfo *MetaDecl
11+
}
12+
13+
func (e DeclData) DeclName() Identifier {
14+
return e.Name
15+
}
16+
17+
func (e DeclData) Meta() *MetaDecl {
18+
return e.MetaInfo
19+
}
20+
21+
func (e DeclData) IsExportedDecl() bool {
22+
return true
23+
}
24+
25+
func MakeDeclData(name Identifier, source *Source) *DeclData {
26+
return &DeclData{
27+
Name: name,
28+
Fields: []DeclField{},
29+
Docs: MakeDocs([]string{}),
30+
MetaInfo: &MetaDecl{
31+
Source: source,
32+
},
33+
}
34+
}
35+
36+
func (e *DeclData) AddField(field DeclField) {
37+
e.Fields = append(e.Fields, field)
38+
}

ast/decl-enum-case.go

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package ast
2+
3+
var _ Decl = DeclEnumCase{}
4+
5+
type DeclEnumCase struct {
6+
Name Identifier
7+
8+
Docs *Docs
9+
MetaInfo *MetaDecl
10+
}
11+
12+
func (e DeclEnumCase) DeclName() Identifier {
13+
return e.Name
14+
}
15+
16+
func (e DeclEnumCase) Meta() *MetaDecl {
17+
return e.MetaInfo
18+
}
19+
20+
func (e DeclEnumCase) IsExportedDecl() bool {
21+
return true
22+
}
23+
24+
func MakeDeclEnumCase(name Identifier) *DeclEnumCase {
25+
return &DeclEnumCase{
26+
Name: name,
27+
MetaInfo: &MetaDecl{},
28+
}
29+
}

ast/decl-enum.go

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package ast
2+
3+
import "fmt"
4+
5+
var _ Decl = DeclEnum{}
6+
7+
type DeclEnum struct {
8+
Name Identifier
9+
Cases []*DeclEnumCase
10+
11+
Docs *Docs
12+
MetaInfo *MetaDecl
13+
}
14+
15+
func (e DeclEnum) DeclName() Identifier {
16+
return e.Name
17+
}
18+
19+
func (e DeclEnum) Meta() *MetaDecl {
20+
return e.MetaInfo
21+
}
22+
23+
func (e DeclEnum) IsExportedDecl() bool {
24+
return true
25+
}
26+
27+
func MakeDeclEnum(name Identifier, source *Source) *DeclEnum {
28+
return &DeclEnum{
29+
Name: name,
30+
Cases: []*DeclEnumCase{},
31+
Docs: MakeDocs([]string{}),
32+
MetaInfo: &MetaDecl{
33+
Source: source,
34+
},
35+
}
36+
}
37+
38+
func (e *DeclEnum) AddCase(case_ *DeclEnumCase) {
39+
e.Cases = append(e.Cases, case_)
40+
}
41+
42+
func (e DeclEnum) String() string {
43+
declarationClause := fmt.Sprintf("enum %s", e.Name)
44+
if len(e.Cases) == 0 {
45+
return declarationClause
46+
}
47+
declarationClause += " { "
48+
for _, caseDecl := range e.Cases {
49+
declarationClause += string(caseDecl.Name) + "; "
50+
}
51+
return declarationClause + "}"
52+
}

ast/decl-extern-func.go

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package ast
2+
3+
var _ Decl = DeclExternFunc{}
4+
5+
type DeclExternFunc struct {
6+
Name Identifier
7+
Parameters []DeclParameter
8+
9+
MetaInfo *MetaDecl
10+
}
11+
12+
func (e DeclExternFunc) DeclName() Identifier {
13+
return e.Name
14+
}
15+
16+
func (e DeclExternFunc) Meta() *MetaDecl {
17+
return e.MetaInfo
18+
}
19+
20+
func (e DeclExternFunc) IsExportedDecl() bool {
21+
return true
22+
}
23+
24+
func MakeDeclExternFunc(name Identifier, params []DeclParameter, source *Source) *DeclExternFunc {
25+
return &DeclExternFunc{
26+
Name: name,
27+
Parameters: params,
28+
MetaInfo: &MetaDecl{source},
29+
}
30+
}

0 commit comments

Comments
 (0)