DEV Community

Pandademic
Pandademic

Posted on • Updated on

Writing a programming language for math in go

#go

OK , ever wanted to make your own language?

Today we make our own math language in GO!

Lets dive right in!

package main
import (
    "fmt"
    "strings"
)

func main() {
    code := "1 + 1" // the MATH -> source code
    selector := strings.Fields(code) // get all the different parts/tokenize
    firstNum := selector[0] // select the first part 
    operator := selector[1] // the middle part(the operator)
    secondNum := selector[2] // last part the second num
        /*
           We have to do a different operation based on what we got as our operator... 
        */
    switch operator {
    case "+": // Its a 'plus' sign ...
        result := int(firstNum) + int(secondNum) // The 'int()' converts the string we got as firstNum to a int
        fmt.Println("Result", result, "\n")
    case "*": // So on!
        result := int(firstNum) * int(secondNum)
        fmt.Println("Result", result, "\n")
    case "/":
        result := int(firstNum) / int(secondNum)
        fmt.Println("Result", result, "\n")
    case "-":
        result := int(firstNum) - int(secondNum)
        fmt.Println("Result", result, "\n")
    }
}
Enter fullscreen mode Exit fullscreen mode

stage one playground link
But we have a few issues:

  • The conversion of string to int doesn't work
  • We can only add 2 numbers That's not good! We cant convert int's to string via this method so we use the strconv package. Here's how we do it:
package main

import (
    "fmt"
    "strconv"
    "strings"
)

func main() {
    code := "1 + 1"                  // the MATH
    selector := strings.Fields(code) // get all the different parts
    oFirstNum := selector[0]         // select the first part
    operator := selector[1]          // the middle part(the operator)
    oSecondNum := selector[2]        // last part the second num // for conversion purposes it's an alias
    // Great but we can't add string so we Convert

    firstNum, _ := strconv.Atoi(oFirstNum) // it can't work it's magic on itself! 

    secondNum, _ := strconv.Atoi(oSecondNum)

    /*
       We have to do a different operation based on what we got as our operator...
    */
    switch operator {
    case "+": // Its a 'plus' sign ...
        result := firstNum + secondNum
        fmt.Println("Result", result, "\n")
    case "*": // So on!
        result := firstNum * secondNum
        fmt.Println("Result", result, "\n")
    case "/":
        result := firstNum / secondNum
        fmt.Println("Result", result, "\n")
    case "-":
        result := firstNum - secondNum
        fmt.Println("Result", result, "\n")
    }
}
Enter fullscreen mode Exit fullscreen mode

stage 2 go playground

however, there are still more caveats to this:

  • our language is not very well defined, and is prone to unspoken, undefined behavior(If you don't know what it is, look it up - a big no no), for example - it implodes without proper spacing between number/symbols

  • it can only do a set number of calculations(1)

  • and it again proceeds to go "boom!" on most errors, try replacing a number in the source that's computed with not a number and see what happens.


However, that's it for today, so attempt to fix these on your own for now, and hold while I write another one soon.

edit: It's been over one year (🥳), and I have been on hiatus on DEV(I'm not going to explain it all here) and so this is on indefinite hold.Feel free to continue this in your own posts, and try and tag me(forgot if DEV has this anymore) if you do.

Ps: Thanks for reading

original published date: Valentines day 2022

Top comments (0)