Skip to content

Commit

Permalink
fixed function arguments and added a check
Browse files Browse the repository at this point in the history
  • Loading branch information
mrredo committed Apr 11, 2023
1 parent dab37d9 commit 8008d52
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 30 deletions.
3 changes: 3 additions & 0 deletions err/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ func NewError(msg string, pos scanner.Position) error {
func Error(errFormat string, errType string, pos scanner.Position) error {
return fmt.Errorf("./%s:%d:%d: %s: %s", pos.Filename, pos.Line, pos.Column, errType, errFormat)
}
func NewUnusedError(variableName string, pos scanner.Position) error {
return Error(fmt.Sprintf(VariableNotUsed, variableName), RefrenceError, pos)
}
func NewUndefinedError(variableName string, pos scanner.Position) error {
return Error(fmt.Sprintf(VariableNotDefined, variableName), RefrenceError, pos)
}
Expand Down
1 change: 1 addition & 0 deletions err/errorMessages.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package err

const (
VariableNotDefined = "'%s' is not defined"
VariableNotUsed = "'%s' is not used"
MismatchedTypesInExpression = "mismatched types in expression"
FunctionReturnsVoid = "'%s' method returns void"
VariableAlreadyDeclared = "'%s' is already declared"
Expand Down
31 changes: 26 additions & 5 deletions interpreter/interpret.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,48 @@
package interpreter

import (
"RedoLanguage/err"
lx "RedoLanguage/lexer"
"RedoLanguage/std"
"log"
"strings"
)

func Interpret(input string) {
if input[len(input)-1] != ';' {
input += ";"
}
// if input[len(input)-1] != ';' {
// input += ";"
// }

lexer := lx.NewLexer(strings.ReplaceAll(input, " ", " "))
var secondTS lx.Token = lexer.NextToken()
curT := secondTS
// var curPos, secPos = lexer.Scanner.Pos(), lexer.Scanner.Pos()
for {

curT = secondTS
// curPos = lexer.Scanner.Pos()

secondT := lexer.NextToken()
if curT.Type == lx.EOF || secondT.Type == lx.EOF {
// secPos = lexer.Scanner.Pos()
if curT.Type == lx.EOF {
break
}

if curT.Type == lx.IDENTIFIER {
_, ok := std.Variables[curT.Value]
_, ok1 := std.Functions[curT.Value]

if !ok && !ok1 {
errs := err.NewUndefinedError(curT.Value, lexer.Scanner.Pos())
log.Println(errs)
break
}
// if curPos.Line != secPos.Line {
// errs := err.NewUnusedError(curT.Value, lexer.Scanner.Pos())
// log.Println(errs)
// break
// }

}
if lx.IsVariableExpression(curT, secondT, lexer) { // key +

key := curT
Expand Down Expand Up @@ -59,6 +79,7 @@ func Interpret(input string) {
}

}

secondTS = secondT
}
}
9 changes: 0 additions & 9 deletions lexer/checking.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,3 @@ func IsVariable(token Token) bool {
return token.Type == VAR
}

//func TestIsVariable() {
// lx := NewLexer(`
//var hello = "hello world"
//`)
// curT := lx.NextToken()
// fmt.Println(IsVariable(curT))
// fmt.Println(ParseVariable(curT, lx))
// //fmt.Println(ParseVariable(lx))
//}
1 change: 0 additions & 1 deletion lexer/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ func (l *Lexer) NextToken() Token {
//var lastLine int
for tok == scanner.Comment || tok == scanner.EOF {
//if l.Scanner.Pos().Line != lastLine { // check if the line number has changed
// fmt.Println("Newline character found")
// lastLine = l.Scanner.Pos().Line // update lastLine to the current line number
//}
if tok == scanner.EOF {
Expand Down
6 changes: 3 additions & 3 deletions lexer/lexerArithmeticOperations.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ func MathExpressionTokensToEnd(c Token, l *Lexer, function ...bool) (string, Tok

}

if p := l.Scanner.Peek(); p == ';' || p == '\n' {
break
}
// if p := l.Scanner.Peek(); p == ';' || p == '\n' {
// break
// }
switch c.Type {
case COMMA:

Expand Down
8 changes: 5 additions & 3 deletions lexer/lexerFunction.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,12 @@ func ParseFunctionCall(curT Token, sec Token, lexer *Lexer) (string, []interface
if errs != nil {
return "", nil, errs
}

tok = l
if out == "()" {
break
}




o, errss := ParseArithmeticExpressions(out, lexer)
Expand All @@ -79,7 +83,6 @@ func ParseFunctionCall(curT Token, sec Token, lexer *Lexer) (string, []interface
}
//tok = lexer.NextToken()
}
//fmt.Println(args)
return funcName, args, nil
}

Expand Down Expand Up @@ -150,5 +153,4 @@ func IsFunction(token Token, secondT Token, lexer *Lexer) bool {
//print()
//`)
// c := lx.NextToken()
// fmt.Println(IsFunction(c, lx))
//}
2 changes: 0 additions & 2 deletions lexer/lexerVariable.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ func RuneToStr(ch rune) string {
func ParseVariable(curToken Token, sec Token, lexer *Lexer) (key string, value interface{}, err error) {
tok := curToken //
keyT := sec
//fmt.Println(tok)
if tok.Type == 0 {
return "", nil, nil
}
Expand All @@ -28,7 +27,6 @@ func ParseVariable(curToken Token, sec Token, lexer *Lexer) (key string, value i
return "", nil, fmt.Errorf("'%s' is already declared", keyT.Value)
}
Eq := lexer.NextToken()
//fmt.Println(Eq)
if Eq.Type != ASSIGN {
return "", nil, fmt.Errorf("'=' sign is expected after the '%s'", keyT.Value)
}
Expand Down
1 change: 0 additions & 1 deletion lexer/lexerVariableOperator.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ func ParseVariableAssigningExpression(key Token, expression Token, value Token,
std.Variables[key.Value] = k.(int) / valI
return k.(int) / valI, nil
case MODULO_ASSIGN:
//fmt.Println(k.(int) % valI)
std.Variables[key.Value] = k.(int) % valI
return k.(int) % valI, nil
case ASSIGN:
Expand Down
7 changes: 1 addition & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,7 @@ import (
)

func main() {
interpreter.Interpret(`
var ns = 10
print(10)
ns += 10
print(ns)
`)

//lx.TestMath()
//n := lx.NewLexer(`(10+10)*10`)
//l := lx.NewLexer("10 + 10 * 10")
Expand Down

0 comments on commit 8008d52

Please sign in to comment.