Share your repls and programming experiences

← Back to all posts
Lexed Calculator!

Right now, this is obviously not finished. It can do basic stuff like: `1 != 2`, and I think I really needed this, because now I have a better point of view on Pie. Before, I really didn't know what to do. This language is called Mathematica, and will be like a 'test place' for content from Pie in `the works`.

# Operators.

This language supports the following operators:
`+` For adding
`-` For subtracting
`*` Multiplactation
`/` Division
`!=` Not equal too (returns true or false.)
`>` Larger Than(returns true or false)
`<` Less Than (returns true or false)

If you want to follow the github repo to see updates and content in the works for Pie. You can check that out here
It won't be just Python, it will be some C++, Ruby, maybe rust, I don't know.

# What I have so far

• Basic math equations
Without a parser or AST I cannot create a full calculator(`2*3/4+5` or something)
• Operators
• Output
It prints true or false or the number.

# What I hope to add.

This will hopefully easy to add.
• `==` Operator (returns true or false)
I already tried this, but I got an error, I copy pasted the code from the `!=` but changed where it checks `i` for `!` to where it check `i` for `=`. I got an error though, and I could not find an answer, please help me with this
• An AST or Parser.
I want to add this, so eventually I could add stuff for Pie. Or learn what it would do.

# What is Pie.

Some of you might be thinking, "He has been talking about Pie this whole time, what is it?"
Well thats pretty self explanatory.

This is an explanation of Pie:
Pie is an interpreted language that prepares beginners for Mid-Level languages, Has a similar Ruby & C++ Syntax. Example:

``````use math
// Comment
out "Hello, World!";
name = get "What is your name? ";
f = sqrt(2)
for (i;i in name){
out i;
}
func greet(name){
return "Hello," + name + "!";
}
greet(name)
// Switch two values using Pointers
A = 10;
B = 12;
func swap(*val1, *val2){
temp = *val1;
*val1 = *val2;
*val2 = temp;
}
swap(&A, &B);``````

Of course, this will all be very hard, and I do not expect it to come out before 2022, I have a lot of work to do.

# Questions

• Is this a real lexer? @fuzzyastrocat
• Can this be parsed easily, or will I have to change some things?
• How to make the operator `==`

# Notice:

This code was built by me from scratch, without stackoverflow or google, and only one module.
Some of you may have seen my other langs (@RayhanADev) and I have to say, I was a bit lost. I used a lot of googling, and it was really buggy.

# Suggestions? Leave them in the comments!

Lel idk what fuzzy said but I think this is kewl! (also you didn’t pingu me I found this xD)

fuzzyastrocat (1463)

You're gonna get copyright infringement with Wolfram Mathematica

No, this is not a real lexer. It's close, but a real lexer wouldn't evaluate as soon as it runs across something. (Look at my language tutorial if you want an example of what I'm talking about)

elipie (323)

@fuzzyastrocat f I worked hard :( lol. I did look at your language, And the lexer temporarily outputs the stuff just right there.

also, look up latin to english and type mathematica, google translate go brrrrr

elipie (323)

@fuzzyastrocat Ok that last comment didn't make sense. This lexer is a test lexer. So it temporarily outputs the stuff exactly once it is seen, kinda like a JIT compiler but JIT lexer, wait... this lexer is LIT lol

fuzzyastrocat (1463)

@elipie Sorry if I sounded like I was downplaying this, it is a good job. But my point is that it doesn't output the stuff. It just uses it right there. The lexer should just output the stuff, not do any calculations. That's the job of the parser/execution.

elipie (323)

@fuzzyastrocat ohhhhhhhh again, I just wanted to write a lexer in this code... so yeah, ofc I will do that with parser and stuff.

realTronsi (842)

@elipie yeah what @fuzzyastrocat said, so for example when your lexer comes across this, it breaks:

``5+2*3``
elipie (323)

@realTronsi ik what a lexer does, I just didn't want to make a full parser this time, I just wanted to create a lexer

elipie (323)

@realTronsi it breaks `5+2*3` into this:

``````NUMBER: 5
OPERATOR: +
NUMBER: 2
OPERATOR: *
NUMBER: 3``````

But this lexer checks the tokens, then executes them right away, because I didn't feel like making a full parser and AST, I wanted this to be a basic excerise

programmeruser (383)

@elipie example of a real language (some snippets came from cookeylang): https://repl.it/@programmeruser/calclang

elipie (323)

@programmeruser oMg dOeS nObOdY kNoW tHaT tHiS iS jUsT A TEST PLEASE, ik this isnt real language.

fuzzyastrocat (1463)

@programmeruser Very good, but one small flaw: grouping should not be its own type of expression, since grouping exists only to alter parsing precedence. Lines 26-31 of parser.js should be:

``````		if (match("open_paren")) {
const expression = expr();
consume('close_paren', 'Expect closing parenthesis');
return expression;
}``````

And then you can remove the checks for grouping in the interpreter.

elipie (323)
###### horrible photoshop skills go brrrrr
TsunamiOrSumth (481)
elipie (323)
###### cute pie wit a santa hat
TsunamiOrSumth (481)

@elipie

elipie (323)
###### not you too :((, why does everyone want to eat stale pie...
TsunamiOrSumth (481)
##### because pie is gud
BobTheTomatoPie (1593)

bruh wont even let me do equations past 2 numbers. You can use eval although it will make hacking this extremely easy, and it would then most likely be unlisted. Not sure what all the checking if equal stuff is about.

elipie (323)

@BobTheTomatoPie dude, its a lexer and its still a work in progress! I get it now and know all I gotta do is send it to the parser.

elipie (323)

@BobTheTomatoPie and did you even check out the other files?

BobTheTomatoPie (1593)

i saw the other files i get what your trying to do but what is the point? Why reinvent the wheel? @elipie

elipie (323)

@BobTheTomatoPie A lexer is how you make a programming language, so this is practice.

TheCheeseCoder (51)

is this supposed to be a bug?

elipie (323)

@TheCheeseCoder right now, it can only do bath math equations, and stuff like that, I will make the parser next.

EpicRaisin (227)

For the `==` operator, you can just do the same check for `!=` but invert the result, because if 2 things are not different, they are the same.