Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
a8fd0de
Slightly improve error and context handling
monstermichl Aug 10, 2025
ff72995
Move default value evaluation to separate function
monstermichl Aug 10, 2025
d20be40
Replace tabs by spaces
monstermichl Aug 10, 2025
c81e3c8
Start struct definition implementation
monstermichl Aug 10, 2025
a1e5d1e
Start preparation for custom defined types
monstermichl Aug 31, 2025
cc7950e
Implement first working draft of type declarations/definitions (slice…
monstermichl Sep 4, 2025
9bef94e
Do some renaming (https://github.com/monstermichl/TypeShell/issues/45)
monstermichl Sep 4, 2025
d00c08a
Do some renaming (https://github.com/monstermichl/TypeShell/issues/45)
monstermichl Sep 4, 2025
d6a9918
Fix type definition base type handling (https://github.com/monstermic…
monstermichl Sep 4, 2025
2fe4046
Add tests (https://github.com/monstermichl/TypeShell/issues/45)
monstermichl Sep 4, 2025
b9b5637
Revert "Start struct definition implementation"
monstermichl Sep 4, 2025
4ec043e
Fix typo (https://github.com/monstermichl/TypeShell/issues/45)
monstermichl Sep 4, 2025
c0e2639
Merge branch 'custom-types' into v0.2.0
monstermichl Sep 4, 2025
be1a2f3
Start constants implementation (https://github.com/monstermichl/TypeS…
monstermichl Sep 4, 2025
529c48b
Use a common function for variable and constant evaluation (evaluateN…
monstermichl Sep 5, 2025
08e90cf
Improve error message handling
monstermichl Sep 5, 2025
635ab1e
Add first working draft of single line const definition (https://gith…
monstermichl Sep 5, 2025
af7dd1d
Fix Const instantiation (https://github.com/monstermichl/TypeShell/is…
monstermichl Sep 5, 2025
c0fa740
Define in Expression if it's constant to be able to decide if the ass…
monstermichl Sep 5, 2025
2831525
Fix expression evaluation (https://github.com/monstermichl/TypeShell/…
monstermichl Sep 5, 2025
c26ce6b
Make sure values are assigned at constant definition (https://github.…
monstermichl Sep 5, 2025
346f334
Make sure value cannot be assigned to named value if it's a constant …
monstermichl Sep 5, 2025
b393eed
Add tests (https://github.com/monstermichl/TypeShell/issues/41)
monstermichl Sep 5, 2025
399da7a
Allow constant and variable definitions via grouping syntax (https://…
monstermichl Sep 5, 2025
beda713
Add simple iota implementation (only simple increment for now) (https…
monstermichl Sep 5, 2025
edcddc1
Add tests (https://github.com/monstermichl/TypeShell/issues/41)
monstermichl Sep 5, 2025
2b8f166
Update README (https://github.com/monstermichl/TypeShell/issues/41)
monstermichl Sep 5, 2025
829aece
Fix typo
monstermichl Sep 5, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 47 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,44 @@ Supported variable types are *bool*, *int*, *string* and *error*.
```golang
// Variable definition with default value.
var a int
var a, b int
var b, c int
```

```golang
// Variable definition with assigned value.
var a int = 5
var a, b int = divisionWithRemainder(5, 2)
var b, c int = divisionWithRemainder(5, 2)
```

```golang
// Variable definition via grouping.
var (
a = 5
b, c int = divisionWithRemainder(5, 2)
)
```

```golang
// Variable definition short form.
a := 5
a, b := 5, 6
a, b := divisionWithRemainder(5, 2)
b, c := 5, 6
d, e := divisionWithRemainder(5, 2)
```

### Constants
```golang
// Constant definition.
const a = 0
const b, c = 1, 2
```

```golang
// Constant definition via grouping.
const (
a = -1
b = iota
c
)
```

### Control flow
Expand Down Expand Up @@ -196,6 +220,25 @@ import (
print(strings.Contains("Hello World", "World")) // Prints 1.
```

### Type declarations
TypeShell supports the declaration of types. However, types which result in slices are not supported yet.

```golang
// Define a type.
type myType int

var a myType
a = myType(24)
```

```golang
// Define an alias.
type myType = int

var a myType
a = 24
```

### Builtin
```golang
// Returns the length of a slice or a string.
Expand Down
22 changes: 6 additions & 16 deletions lexer/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ const (
STRING_LITERAL
NIL_LITERAL

// Types.
DATA_TYPE

// Separators.
COMMA
COLON
Expand All @@ -58,6 +55,8 @@ const (

// Keywords.
IMPORT
TYPE_DECLARATION
CONST_DEFINITION
VAR_DEFINITION
FUNCTION_DEFINITION
RETURN
Expand All @@ -70,6 +69,7 @@ const (
RANGE
BREAK
CONTINUE
IOTA

// Builtin functions.
LEN
Expand All @@ -90,13 +90,6 @@ const (
EOF
)

const (
DATA_TYPE_BOOLEAN VarType = "bool"
DATA_TYPE_INTEGER VarType = "int"
DATA_TYPE_STRING VarType = "string"
DATA_TYPE_ERROR VarType = "error"
)

type Token struct {
tokenType TokenType
value string
Expand Down Expand Up @@ -180,6 +173,8 @@ var nonAlphabeticTokens = []tokenMapping{
var keywords = map[string]TokenType{
// Common keywords.
"import": IMPORT,
"type": TYPE_DECLARATION,
"const": CONST_DEFINITION,
"var": VAR_DEFINITION,
"func": FUNCTION_DEFINITION,
"return": RETURN,
Expand All @@ -192,6 +187,7 @@ var keywords = map[string]TokenType{
"range": RANGE,
"break": BREAK,
"continue": CONTINUE,
"iota": IOTA,
"nil": NIL_LITERAL,

// Builtin functions.
Expand All @@ -204,12 +200,6 @@ var keywords = map[string]TokenType{
"read": READ,
"write": WRITE,
"panic": PANIC,

// Types.
DATA_TYPE_BOOLEAN: DATA_TYPE,
DATA_TYPE_INTEGER: DATA_TYPE,
DATA_TYPE_STRING: DATA_TYPE,
DATA_TYPE_ERROR: DATA_TYPE,
}

func newToken(value string, tokenType TokenType, row int, column int) Token {
Expand Down
4 changes: 4 additions & 0 deletions parser/appcall.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ func (a AppCall) ValueType() ValueType {
return NewValueType(DATA_TYPE_MULTIPLE, false)
}

func (a AppCall) IsConstant() bool {
return false
}

func (a AppCall) Name() string {
return a.name
}
Expand Down
6 changes: 6 additions & 0 deletions parser/assignment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package parser

type Assignment interface {
Statement
AssignmentType() AssignmentType
}
10 changes: 7 additions & 3 deletions parser/binaryoperation.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package parser

type BinaryOperation struct {
left Expression
operator BinaryOperator
right Expression
left Expression
operator BinaryOperator
right Expression
}

func (b BinaryOperation) StatementType() StatementType {
Expand All @@ -14,6 +14,10 @@ func (b BinaryOperation) ValueType() ValueType {
return b.left.ValueType()
}

func (b BinaryOperation) IsConstant() bool {
return b.Left().IsConstant() && b.Right().IsConstant()
}

func (b BinaryOperation) Left() Expression {
return b.left
}
Expand Down
4 changes: 4 additions & 0 deletions parser/comparison.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ func (c Comparison) ValueType() ValueType {
return ValueType{dataType: DATA_TYPE_BOOLEAN}
}

func (c Comparison) IsConstant() bool {
return c.Left().IsConstant() && c.Right().IsConstant()
}

func (c Comparison) Left() Expression {
return c.left
}
Expand Down
70 changes: 70 additions & 0 deletions parser/const.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package parser

type Const struct {
name string
valueType ValueType
global bool
public bool
}

func NewConst(name string, valueType ValueType, global bool, public bool) Const {
return Const{
name,
valueType,
global,
public,
}
}

func (c Const) Name() string {
return c.name
}

func (c Const) ValueType() ValueType {
return c.valueType
}

func (c Const) IsConstant() bool {
return true
}

func (c *Const) SetValueType(valueType ValueType) {
c.valueType = valueType
}

func (c Const) Global() bool {
return c.global
}

func (c Const) Public() bool {
return c.public
}

type ConstDefinition struct {
constants []Const
values []Expression
}

func (c ConstDefinition) StatementType() StatementType {
return STATEMENT_TYPE_CONST_DEFINITION
}

func (c ConstDefinition) AssignmentType() AssignmentType {
return ASSIGNMENT_TYPE_VALUE
}

func (c ConstDefinition) Constants() []Const {
return c.constants
}

func (c ConstDefinition) Values() []Expression {
return c.values
}

type ConstEvaluation struct {
Const
}

func (c ConstEvaluation) StatementType() StatementType {
return STATEMENT_TYPE_CONST_EVALUATION
}
4 changes: 4 additions & 0 deletions parser/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ func (c Copy) ValueType() ValueType {
return NewValueType(DATA_TYPE_INTEGER, false)
}

func (c Copy) IsConstant() bool {
return false
}

func (c Copy) Source() Expression {
return c.source
}
Expand Down
4 changes: 4 additions & 0 deletions parser/exists.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ func (e Exists) ValueType() ValueType {
return NewValueType(DATA_TYPE_BOOLEAN, false)
}

func (e Exists) IsConstant() bool {
return false
}

func (e Exists) Path() Expression {
return e.path
}
1 change: 1 addition & 0 deletions parser/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ type Expression interface {
// An expression is a super-type of statement which results in a value.
Statement
ValueType() ValueType
IsConstant() bool
}
8 changes: 8 additions & 0 deletions parser/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ func (e FunctionDefinition) ValueType() ValueType {
return functionValueType(e.returnTypes)
}

func (e FunctionDefinition) IsConstant() bool {
return false
}

func (e FunctionDefinition) ReturnTypes() []ValueType {
return e.returnTypes
}
Expand Down Expand Up @@ -54,6 +58,10 @@ func (e FunctionCall) ValueType() ValueType {
return functionValueType(e.returnTypes)
}

func (e FunctionCall) IsConstant() bool {
return false
}

func (e FunctionCall) ReturnTypes() []ValueType {
return e.returnTypes
}
Expand Down
4 changes: 4 additions & 0 deletions parser/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ func (e Group) ValueType() ValueType {
return e.Child().ValueType()
}

func (e Group) IsConstant() bool {
return e.Child().IsConstant()
}

func (e Group) Child() Expression {
return e.child
}
4 changes: 4 additions & 0 deletions parser/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ func (i Input) ValueType() ValueType {
return ValueType{dataType: DATA_TYPE_STRING}
}

func (i Input) IsConstant() bool {
return false
}

func (i Input) Prompt() Expression {
return i.prompt
}
16 changes: 16 additions & 0 deletions parser/iota.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package parser

type Iota struct {
}

func (i Iota) StatementType() StatementType {
return STATEMENT_TYPE_IOTA
}

func (i Iota) ValueType() ValueType {
return NewValueType(DATA_TYPE_INTEGER, false)
}

func (i Iota) IsConstant() bool {
return true
}
4 changes: 4 additions & 0 deletions parser/itoa.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ func (e Itoa) ValueType() ValueType {
return NewValueType(DATA_TYPE_STRING, false)
}

func (o Itoa) IsConstant() bool {
return false
}

func (e Itoa) Value() Expression {
return e.value
}
4 changes: 4 additions & 0 deletions parser/len.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ func (l Len) ValueType() ValueType {
return NewValueType(DATA_TYPE_INTEGER, false)
}

func (l Len) IsConstant() bool {
return false
}

func (l Len) Expression() Expression {
return l.expression
}
Loading
Loading