Adding Assignment Expressions To The AST - Programming Language From Scratch

  Рет қаралды 3,912

tylerlaceby

tylerlaceby

Жыл бұрын

This episode covers the topics of assignment expressions and how to evaluate them inside the interpreter.
We will also re-work the null type and add two more types true and false.
Source: github.com/tlaceby/guide-to-i...
🔔 JOIN THE COMMUNITY 🔔
kzfaq.info... -- Subscribe xD
----------------------------------------
-- Social Links --
Discord - / discord
GitHub - github.com/tlaceby/

Пікірлер: 15
@MisterDan
@MisterDan Жыл бұрын
Nicely explained. Thank you!
@account196
@account196 Жыл бұрын
Excelent series! As I'm following along I couldn't help but notice that when you declare a variable you need to put the semicolon at the end, but when you reassign it you can't have it. I've tried to implement it myself but I lost the ability to chain multiple equals like a = b = c = 3. (sorry if that's something that was addressed in a later episode, I still haven't watched everything yet)
@tylerlaceby
@tylerlaceby Жыл бұрын
Totally great observation. Chained syntax won’t work this way. You will want to create a ParseExpressionStatement() function which gets called after checking all possible statements. It will simply parse a expression then expect a semicolon. No expressions therefore would need the check for semicolons.
@PrivacyNProgramming
@PrivacyNProgramming 2 ай бұрын
I actually (somewhat) managed to do what you described (making sure assigning variables means you need an ending semicolon), i did in C#, for my programming language, it works well (atleast on the tests ive done) Although its in C#, here is the basic steps; In your Parse, go to your parse_primary_expr(), and before checking for token types and all, add this statement; (C#) if (currentToken == TokenType.SemiColon && tokens[tokens.Count - 1].Type == TokenType.EOF) { this.eat(); return null; } then in the Interpreter, go to the evaluate function, and add a null check at the start, if astNode is null, return null; if (astNode == null) { return null; } and that should be it! This should allow you to force ending semi colon while allowing chaining of variables, (I will re-edit if i missed something)
@Tom_Bird
@Tom_Bird 4 ай бұрын
Very nice tutorials, I wonder why we declare an expression that is identical to an statement. Why not just statement?
@tylerlaceby
@tylerlaceby 4 ай бұрын
Think of a line of code where you do print(“hello”); This is an expression with a semicolon. This is what we call an expressionStmt. The point of this is to allow statements with just expressions. If we didn’t have such statements then checking for semicolons would be tricker. Also it allows us to do shorthand if statements or for loops since we just require any statement after the condition.
@tsurumaruwordpress
@tsurumaruwordpress Жыл бұрын
4 videos in one day! Just curious why assignment is an expression and not a statement. I wouldn't think it would matter, but intuitively I would assume it wouldn't return anything and hence you could code it as a statement.
@mariansam_was_taken
@mariansam_was_taken Жыл бұрын
I guess if you want something like `foo = bar = baz = 5` work, then it gets parsed kinda like `foo = (bar = (baz = 5))`, which indeed means that `baz = 5` has to return 5 in order for `bar = ...` to work. I think some languages use this when doing something like `while (line = readLineFromFile()) { ... }` where the `=` both assigns the read line to `line` and then gives it to the `while` keyword, which either continues in the loop or breaks out of it.
@tsurumaruwordpress
@tsurumaruwordpress Жыл бұрын
@@mariansam_was_taken thank you! Come to think of it I believe that was explained in the video. Guess it just takes me a bit more to keep up.
@fuadcs22
@fuadcs22 2 ай бұрын
Hello Sir, I have a question. What if I dont want semicolons in the language and what about assigning to false = 'value'. This should be a different error, like 'false' cannot be re-assigned. Thank you for your great videos.
@tylerlaceby
@tylerlaceby 2 ай бұрын
Then you need to change the error. I like semicolons hence why I am requiring them here. However if you don’t want them you don’t need them.
@TTCBlaze
@TTCBlaze Жыл бұрын
lol you filmed this on halloween
@mrmaniac9905
@mrmaniac9905 11 ай бұрын
Rather silly to treat boolean and null literals as identifiers..
@mrmaniac9905
@mrmaniac9905 11 ай бұрын
or variables rather... kinda defeats the point of it being a "literal" lmao
@tylerlaceby
@tylerlaceby 11 ай бұрын
Hey your the one who is clearly vested in giving feedback and I’m appreciative however if you keep writing comments calling my approach silly I will simply ignore you. This is literally what some languages do. They don’t have a AST node for true or false as those are simply there own thing. So before you critique time for doing so be sure you don’t look like a idiot as this is what some notable languages have done in their AST generation. Hell common C compilers do not differentiate between these just like I have. It’s much more work to create a NullLiteral, BooleanLitersl AST node then simply adding three lines in the a type checker. You could simply define true and false as Booleans and be fine.
Just try to use a cool gadget 😍
00:33
123 GO! SHORTS
Рет қаралды 85 МЛН
WHO DO I LOVE MOST?
00:22
dednahype
Рет қаралды 78 МЛН
МАМА И STANDOFF 2 😳 !FAKE GUN! #shorts
00:34
INNA SERG
Рет қаралды 4,1 МЛН
MEU IRMÃO FICOU FAMOSO
00:52
Matheus Kriwat
Рет қаралды 38 МЛН
Native Global Functions - Programming Language From Scratch
19:12
tylerlaceby
Рет қаралды 3,4 М.
STRUCTS  In GO Lang
17:28
Code Alley with Anurag
Рет қаралды 138
Vim Tips I Wish I Knew Earlier
23:00
Sebastian Daschner
Рет қаралды 40 М.
Walking The AST - Programming Language From Scratch
25:43
tylerlaceby
Рет қаралды 13 М.
YOTAPHONE 2 - СПУСТЯ 10 ЛЕТ
15:13
ЗЕ МАККЕРС
Рет қаралды 140 М.
#miniphone
0:16
Miniphone
Рет қаралды 3,7 МЛН
Low Price Best 👌 China Mobile 📱
0:42
Tech Official
Рет қаралды 717 М.
iPhone 16 с инновационным аккумулятором
0:45
ÉЖИ АКСЁНОВ
Рет қаралды 519 М.