Minimal addition language in 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
selector := strings.Fields(code) // get a…


This content originally appeared on DEV Community and was authored by Pandademic

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
    selector := strings.Fields(code) // get all the different parts
    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")
    }
}

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")
    }
}

stage 2 go playground

That's it for today! I'll write another article soon improving MATHLANG somemore!

Ps: Thanks for reading


This content originally appeared on DEV Community and was authored by Pandademic


Print Share Comment Cite Upload Translate Updates
APA

Pandademic | Sciencx (2022-02-14T22:24:29+00:00) Minimal addition language in go. Retrieved from https://www.scien.cx/2022/02/14/minimal-addition-language-in-go/

MLA
" » Minimal addition language in go." Pandademic | Sciencx - Monday February 14, 2022, https://www.scien.cx/2022/02/14/minimal-addition-language-in-go/
HARVARD
Pandademic | Sciencx Monday February 14, 2022 » Minimal addition language in go., viewed ,<https://www.scien.cx/2022/02/14/minimal-addition-language-in-go/>
VANCOUVER
Pandademic | Sciencx - » Minimal addition language in go. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/02/14/minimal-addition-language-in-go/
CHICAGO
" » Minimal addition language in go." Pandademic | Sciencx - Accessed . https://www.scien.cx/2022/02/14/minimal-addition-language-in-go/
IEEE
" » Minimal addition language in go." Pandademic | Sciencx [Online]. Available: https://www.scien.cx/2022/02/14/minimal-addition-language-in-go/. [Accessed: ]
rf:citation
» Minimal addition language in go | Pandademic | Sciencx | https://www.scien.cx/2022/02/14/minimal-addition-language-in-go/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.