Skip to content

Commit 04f1503

Browse files
authored
Custom types (#47)
* Slightly improve error and context handling * Move default value evaluation to separate function * Replace tabs by spaces * Start struct definition implementation * Start preparation for custom defined types (#45) * Implement first working draft of type declarations/definitions (slices not yet supported) (#45) * Do some renaming (#45) * Do some renaming (#45) * Fix type definition base type handling (#45) * Add tests (#45) * Revert "Start struct definition implementation" This reverts commit c81e3c8. * Fix typo (#45) * Start constants implementation (#41) * Use a common function for variable and constant evaluation (evaluateNamedValueDefinition) (#41) * Improve error message handling * Add first working draft of single line const definition (#41) * Fix Const instantiation (#41) * Define in Expression if it's constant to be able to decide if the assigned value to a constant is actually constant (#41) * Fix expression evaluation (#41) * Make sure values are assigned at constant definition (#41) * Make sure value cannot be assigned to named value if it's a constant (#41) * Add tests (#41) * Allow constant and variable definitions via grouping syntax (#41) * Add simple iota implementation (only simple increment for now) (#41) * Add tests (#41) * Update README (#41) * Fix typo
1 parent d86389a commit 04f1503

35 files changed

Lines changed: 1608 additions & 336 deletions

README.md

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,44 @@ Supported variable types are *bool*, *int*, *string* and *error*.
3030
```golang
3131
// Variable definition with default value.
3232
var a int
33-
var a, b int
33+
var b, c int
3434
```
3535

3636
```golang
3737
// Variable definition with assigned value.
3838
var a int = 5
39-
var a, b int = divisionWithRemainder(5, 2)
39+
var b, c int = divisionWithRemainder(5, 2)
40+
```
41+
42+
```golang
43+
// Variable definition via grouping.
44+
var (
45+
a = 5
46+
b, c int = divisionWithRemainder(5, 2)
47+
)
4048
```
4149

4250
```golang
4351
// Variable definition short form.
4452
a := 5
45-
a, b := 5, 6
46-
a, b := divisionWithRemainder(5, 2)
53+
b, c := 5, 6
54+
d, e := divisionWithRemainder(5, 2)
55+
```
56+
57+
### Constants
58+
```golang
59+
// Constant definition.
60+
const a = 0
61+
const b, c = 1, 2
62+
```
63+
64+
```golang
65+
// Constant definition via grouping.
66+
const (
67+
a = -1
68+
b = iota
69+
c
70+
)
4771
```
4872

4973
### Control flow
@@ -196,6 +220,25 @@ import (
196220
print(strings.Contains("Hello World", "World")) // Prints 1.
197221
```
198222

223+
### Type declarations
224+
TypeShell supports the declaration of types. However, types which result in slices are not supported yet.
225+
226+
```golang
227+
// Define a type.
228+
type myType int
229+
230+
var a myType
231+
a = myType(24)
232+
```
233+
234+
```golang
235+
// Define an alias.
236+
type myType = int
237+
238+
var a myType
239+
a = 24
240+
```
241+
199242
### Builtin
200243
```golang
201244
// Returns the length of a slice or a string.

lexer/lexer.go

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,6 @@ const (
4242
STRING_LITERAL
4343
NIL_LITERAL
4444

45-
// Types.
46-
DATA_TYPE
47-
4845
// Separators.
4946
COMMA
5047
COLON
@@ -58,6 +55,8 @@ const (
5855

5956
// Keywords.
6057
IMPORT
58+
TYPE_DECLARATION
59+
CONST_DEFINITION
6160
VAR_DEFINITION
6261
FUNCTION_DEFINITION
6362
RETURN
@@ -70,6 +69,7 @@ const (
7069
RANGE
7170
BREAK
7271
CONTINUE
72+
IOTA
7373

7474
// Builtin functions.
7575
LEN
@@ -90,13 +90,6 @@ const (
9090
EOF
9191
)
9292

93-
const (
94-
DATA_TYPE_BOOLEAN VarType = "bool"
95-
DATA_TYPE_INTEGER VarType = "int"
96-
DATA_TYPE_STRING VarType = "string"
97-
DATA_TYPE_ERROR VarType = "error"
98-
)
99-
10093
type Token struct {
10194
tokenType TokenType
10295
value string
@@ -180,6 +173,8 @@ var nonAlphabeticTokens = []tokenMapping{
180173
var keywords = map[string]TokenType{
181174
// Common keywords.
182175
"import": IMPORT,
176+
"type": TYPE_DECLARATION,
177+
"const": CONST_DEFINITION,
183178
"var": VAR_DEFINITION,
184179
"func": FUNCTION_DEFINITION,
185180
"return": RETURN,
@@ -192,6 +187,7 @@ var keywords = map[string]TokenType{
192187
"range": RANGE,
193188
"break": BREAK,
194189
"continue": CONTINUE,
190+
"iota": IOTA,
195191
"nil": NIL_LITERAL,
196192

197193
// Builtin functions.
@@ -204,12 +200,6 @@ var keywords = map[string]TokenType{
204200
"read": READ,
205201
"write": WRITE,
206202
"panic": PANIC,
207-
208-
// Types.
209-
DATA_TYPE_BOOLEAN: DATA_TYPE,
210-
DATA_TYPE_INTEGER: DATA_TYPE,
211-
DATA_TYPE_STRING: DATA_TYPE,
212-
DATA_TYPE_ERROR: DATA_TYPE,
213203
}
214204

215205
func newToken(value string, tokenType TokenType, row int, column int) Token {

parser/appcall.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ func (a AppCall) ValueType() ValueType {
1414
return NewValueType(DATA_TYPE_MULTIPLE, false)
1515
}
1616

17+
func (a AppCall) IsConstant() bool {
18+
return false
19+
}
20+
1721
func (a AppCall) Name() string {
1822
return a.name
1923
}

parser/assignment.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package parser
2+
3+
type Assignment interface {
4+
Statement
5+
AssignmentType() AssignmentType
6+
}

parser/binaryoperation.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package parser
22

33
type BinaryOperation struct {
4-
left Expression
5-
operator BinaryOperator
6-
right Expression
4+
left Expression
5+
operator BinaryOperator
6+
right Expression
77
}
88

99
func (b BinaryOperation) StatementType() StatementType {
@@ -14,6 +14,10 @@ func (b BinaryOperation) ValueType() ValueType {
1414
return b.left.ValueType()
1515
}
1616

17+
func (b BinaryOperation) IsConstant() bool {
18+
return b.Left().IsConstant() && b.Right().IsConstant()
19+
}
20+
1721
func (b BinaryOperation) Left() Expression {
1822
return b.left
1923
}

parser/comparison.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ func (c Comparison) ValueType() ValueType {
2222
return ValueType{dataType: DATA_TYPE_BOOLEAN}
2323
}
2424

25+
func (c Comparison) IsConstant() bool {
26+
return c.Left().IsConstant() && c.Right().IsConstant()
27+
}
28+
2529
func (c Comparison) Left() Expression {
2630
return c.left
2731
}

parser/const.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package parser
2+
3+
type Const struct {
4+
name string
5+
valueType ValueType
6+
global bool
7+
public bool
8+
}
9+
10+
func NewConst(name string, valueType ValueType, global bool, public bool) Const {
11+
return Const{
12+
name,
13+
valueType,
14+
global,
15+
public,
16+
}
17+
}
18+
19+
func (c Const) Name() string {
20+
return c.name
21+
}
22+
23+
func (c Const) ValueType() ValueType {
24+
return c.valueType
25+
}
26+
27+
func (c Const) IsConstant() bool {
28+
return true
29+
}
30+
31+
func (c *Const) SetValueType(valueType ValueType) {
32+
c.valueType = valueType
33+
}
34+
35+
func (c Const) Global() bool {
36+
return c.global
37+
}
38+
39+
func (c Const) Public() bool {
40+
return c.public
41+
}
42+
43+
type ConstDefinition struct {
44+
constants []Const
45+
values []Expression
46+
}
47+
48+
func (c ConstDefinition) StatementType() StatementType {
49+
return STATEMENT_TYPE_CONST_DEFINITION
50+
}
51+
52+
func (c ConstDefinition) AssignmentType() AssignmentType {
53+
return ASSIGNMENT_TYPE_VALUE
54+
}
55+
56+
func (c ConstDefinition) Constants() []Const {
57+
return c.constants
58+
}
59+
60+
func (c ConstDefinition) Values() []Expression {
61+
return c.values
62+
}
63+
64+
type ConstEvaluation struct {
65+
Const
66+
}
67+
68+
func (c ConstEvaluation) StatementType() StatementType {
69+
return STATEMENT_TYPE_CONST_EVALUATION
70+
}

parser/copy.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ func (c Copy) ValueType() ValueType {
1313
return NewValueType(DATA_TYPE_INTEGER, false)
1414
}
1515

16+
func (c Copy) IsConstant() bool {
17+
return false
18+
}
19+
1620
func (c Copy) Source() Expression {
1721
return c.source
1822
}

parser/exists.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ func (e Exists) ValueType() ValueType {
1212
return NewValueType(DATA_TYPE_BOOLEAN, false)
1313
}
1414

15+
func (e Exists) IsConstant() bool {
16+
return false
17+
}
18+
1519
func (e Exists) Path() Expression {
1620
return e.path
1721
}

parser/expression.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ type Expression interface {
44
// An expression is a super-type of statement which results in a value.
55
Statement
66
ValueType() ValueType
7+
IsConstant() bool
78
}

0 commit comments

Comments
 (0)