cpp-programming

Simple C++ programs
git clone git://git.laack.co/cpp-programming.git
Log | Files | Refs | README

commit 6c303216dfa625893501e10af37644b53d3c4b5b
parent d19ab0f0445f5cb1dfe91bf24badfd53129b3f34
Author: Andrew Laack <andrew@laack.co>
Date:   Mon,  9 Nov 2026 16:10:23 -0600

Setting up expression handling

Diffstat:
Alox/expression.cpp | 45+++++++++++++++++++++++++++++++++++++++++++++
Mlox/main.lox | 6+-----
Mlox/stub.cpp | 1+
Mlox/token.cpp | 2++
4 files changed, 49 insertions(+), 5 deletions(-)

diff --git a/lox/expression.cpp b/lox/expression.cpp @@ -0,0 +1,45 @@ +enum class ExprType { + BINARY, + UNARY, + LITERAL, + GROUPING +}; + +class Expr { + public: + Expr(ExprType tp, Token op, Expr* left, Expr* right) { + type = tp; + this->op = op; + leftExpr = left; + rightExpr = right; + } + + int getOperandCount() { + int num = 0; + if(leftExpr != nullptr) { + num += 1; + } + if(rightExpr != nullptr) { + num += 1; + } + return num; + } + + Expr* getLeft() { + return leftExpr; // may be nullptr + } + + Expr* getRight() { + return rightExpr; // may be nullptr + } + + ExprType getType () { + return type; + } + + private: + ExprType type; + Token op; + Expr* leftExpr; + Expr* rightExpr; +}; diff --git a/lox/main.lox b/lox/main.lox @@ -1,5 +1 @@ -/* This is -a multi -line comment*/ - -/**/numfirst = 10.1 +-10 diff --git a/lox/stub.cpp b/lox/stub.cpp @@ -9,6 +9,7 @@ #include "token.cpp" #include "scanner.cpp" +#include "expression.cpp" #include "lox.cpp" #include "main.cpp" diff --git a/lox/token.cpp b/lox/token.cpp @@ -125,6 +125,8 @@ class Token { std::string lexeme; Literal literal; int line; + Token() { + } Token(TokenType tp, std::string lex, Literal li, int ln) { type = tp; lexeme = lex;