Skip to content

Commit

Permalink
Added better static typing support for numbers, i8, i16, i32, i64, f3…
Browse files Browse the repository at this point in the history
…2, f64.
  • Loading branch information
Jamlie committed Nov 5, 2023
1 parent 228b2a2 commit 8b92337
Show file tree
Hide file tree
Showing 11 changed files with 293 additions and 82 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ It has a very small standard library, which contains:

## Install
```sh
$ go install github.com/Jamlie/Jamlang@latest # or github.com/Jamlie/[email protected].0
$ go install github.com/Jamlie/Jamlang@latest # or github.com/Jamlie/[email protected].1
```

## How to add native functions to it?
Expand Down
8 changes: 6 additions & 2 deletions ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,12 @@ func (p *Program) ToString() string {
type VariableType string
const (
StringType VariableType = "string"
NumberType VariableType = "number"
Int8Type VariableType = "i8"
Int16Type VariableType = "i16"
Int32Type VariableType = "i32"
Int64Type VariableType = "i64"
Float32Type VariableType = "f32"
Float64Type VariableType = "f64"
BoolType VariableType = "bool"
ObjectType VariableType = "object"
ArrayType VariableType = "array"
Expand All @@ -78,7 +83,6 @@ type VariableDeclaration struct {
Identifier string
Value Expression
Type VariableType
// IsVar bool
}

func (v *VariableDeclaration) Kind() NodeType {
Expand Down
Binary file modified bin/jamlang.exe
Binary file not shown.
Binary file modified bin/jamlang.out
Binary file not shown.
4 changes: 3 additions & 1 deletion jamlang/callMain.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func Repl(env *runtimelang.Environment) {
program := parser.ProduceAST(text)
runtimeValue, err := runtimelang.Evaluate(&program, *env)
if err != nil {
fmt.Println(err)
fmt.Fprintln(os.Stderr, err)
os.Exit(0)
}
fmt.Println(runtimeValue.Get())
Expand Down Expand Up @@ -77,8 +77,10 @@ func CallMain(env *runtimelang.Environment) {
fmt.Println("Options:")
fmt.Println(" -r\t\tRun a file")
fmt.Println(" -h\t\tShow this help message")
fmt.Println(" -i\t\tInstall a package")
fmt.Println(" run\t\tRun a file")
fmt.Println(" help\t\tShow this help message")
fmt.Println(" install\tInstall a package")
} else if *installFlag {
if len(args) < 1 {
fmt.Println("No package specified")
Expand Down
4 changes: 2 additions & 2 deletions lexer/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ func Tokenize(sourceCode string) []Token {
}

if len(src) == 0 {
fmt.Println("Error: Unterminated string")
fmt.Fprintln(os.Stderr, "Error: Unterminated string")
os.Exit(0)
}

Expand Down Expand Up @@ -232,7 +232,7 @@ func Tokenize(sourceCode string) []Token {
} else if isWhitespace(src[0]) {
src = src[1:]
} else {
fmt.Println("Error: Invalid character '" + string(src[0]) + "'")
fmt.Fprintln(os.Stderr, "Error: Invalid character '" + string(src[0]) + "'")
os.Exit(0)
}
}
Expand Down
31 changes: 23 additions & 8 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ func (p *Parser) parseStatement() ast.Statement {
return p.parseFunctionDeclaration()
case tokentype.Return:
if !p.isFunction {
fmt.Println("Error: Return statement outside of function")
fmt.Fprintln(os.Stderr, "Error: Return statement outside of function")
os.Exit(0)
}
return p.parseReturnStatement()
case tokentype.Class:
return p.parseClassDeclaration()
case tokentype.Break:
if !p.isLoop {
fmt.Println("Error: Break statement outside of loop")
fmt.Fprintln(os.Stderr, "Error: Break statement outside of loop")
os.Exit(0)
}
return p.parseBreakStatement()
Expand Down Expand Up @@ -284,9 +284,24 @@ func (p *Parser) parseType() ast.VariableType {
case "str":
p.eat()
return ast.StringType
case "number":
case "i8":
p.eat()
return ast.NumberType
return ast.Int8Type
case "i16":
p.eat()
return ast.Int16Type
case "i32":
p.eat()
return ast.Int32Type
case "i64":
p.eat()
return ast.Int64Type
case "f32":
p.eat()
return ast.Float32Type
case "f64":
p.eat()
return ast.Float64Type
case "bool":
p.eat()
return ast.BoolType
Expand Down Expand Up @@ -669,7 +684,7 @@ func (p *Parser) parseMemberExpression() ast.Expression {
property = p.parsePrimaryExpression()

if property.Kind() != ast.IdentifierType {
fmt.Println("Expected identifier after '.'")
fmt.Fprintln(os.Stderr, "Expected identifier after '.'")
os.Exit(0)
return nil
}
Expand Down Expand Up @@ -700,7 +715,7 @@ func (p *Parser) parsePrimaryExpression() ast.Expression {
case tokentype.Number:
value, err := strconv.ParseFloat(p.eat().Value, 64)
if err != nil {
fmt.Println(err.Error())
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(0)
return nil
}
Expand Down Expand Up @@ -729,7 +744,7 @@ func (p *Parser) parsePrimaryExpression() ast.Expression {
case tokentype.Function:
return p.parseFunctionDeclaration()
default:
fmt.Println("Unexpected token found: ", p.at())
fmt.Fprintln(os.Stderr, "Unexpected token found: ", p.at())
os.Exit(0)
return nil
}
Expand All @@ -751,7 +766,7 @@ func (p *Parser) peek() lexer.Token {

func (p *Parser) expect(token tokentype.TokenType, message string) lexer.Token {
if p.at().Type != token {
fmt.Println(message)
fmt.Fprintln(os.Stderr, message)
os.Exit(0)
}
return p.eat()
Expand Down
Loading

0 comments on commit 8b92337

Please sign in to comment.