cpp-programming

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

commit 122d645d8136c4e830f7f3a89003869b34e97bd2
parent 5a27bf67e5f366ec26c9a57899671fbae1d98e3c
Author: Andrew Laack <andrew@laack.co>
Date:   Tue, 20 Oct 2026 19:53:44 -0500

Stub build

Diffstat:
Mlox/Makefile | 2+-
Mlox/lox.cpp | 69++++++++++++++++++++++++++++++++-------------------------------------
Alox/lox.hpp | 10++++++++++
Mlox/main.cpp | 7-------
Mlox/main.lox | 3+--
Mlox/scanner.cpp | 85+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------
Alox/stub.cpp | 13+++++++++++++
Mlox/token.cpp | 10++++++++++
8 files changed, 146 insertions(+), 53 deletions(-)

diff --git a/lox/Makefile b/lox/Makefile @@ -1,4 +1,4 @@ build: - g++ main.cpp -o lox.out + g++ stub.cpp -o lox.out run: build ./lox.out main.lox diff --git a/lox/lox.cpp b/lox/lox.cpp @@ -1,39 +1,34 @@ -#include "scanner.cpp" - -class Lox { - public: - void execStr(std::string input) { - run(input); - if(hadError) { - exit(65); - } +void Lox::execStr(std::string input) { + run(input); + if(hadError) { + exit(65); + } +} +void Lox::repl() { + while(true) { + printf("lox> "); + std::string inp; + if (!std::getline(std::cin, inp)) { + std::cout << std::endl; + break; } - void repl() { - while(true) { - printf("lox> "); - std::string inp; - if (!std::getline(std::cin, inp)) { - std::cout << std::endl; - break; - } - run(inp + "\n"); - hadError = false; - } + run(inp + "\n"); + hadError = false; + } +} +void Lox::run(std::string inputStr) { + Scanner scanner = Scanner(inputStr, this); + std::vector<Token> tokens = scanner.scanTokens(); + if(!hadError) { + for(auto& token: tokens) { + std::cout << token.toString() << std::endl; } - private: - bool hadError = false; - void run(std::string inputStr) { - Scanner scanner = Scanner(inputStr); - std::vector<Token> tokens = scanner.scanTokens(); - for(auto& token: tokens) { - std::cout << token.toString() << std::endl; - } - } - void error(int line, std::string message) { - report(line, "", message); - } - void report(int line, std::string where, std::string message) { - printf("[line %d] Error%s: %s", line, where, message); - hadError = true; - } -}; + } +} +void Lox::error(int line, std::string message) { + report(line, "", message); +} +void Lox::report(int line, std::string where, std::string message) { + std::cout << "[line " << line << "] Error" << where << ": " << message << std::endl; + hadError = true; +} diff --git a/lox/lox.hpp b/lox/lox.hpp @@ -0,0 +1,10 @@ +class Lox { + public: + void execStr(std::string input); + void repl(); + void error(int line, std::string message); + private: + bool hadError = false; + void run(std::string inputStr); + void report(int line, std::string where, std::string message); +}; diff --git a/lox/main.cpp b/lox/main.cpp @@ -1,10 +1,3 @@ -#include <stdio.h> -#include <iostream> -#include <fstream> -#include <filesystem> -#include <vector> -#include "lox.cpp" - std::string readFileToString(std::string filePath) { auto in = std::ifstream(filePath); diff --git a/lox/main.lox b/lox/main.lox @@ -1,2 +1 @@ -var language = "lox" -print language +(){}() diff --git a/lox/scanner.cpp b/lox/scanner.cpp @@ -1,13 +1,86 @@ -#include "token.cpp" - class Scanner { public: - Scanner(std::string input) { - inp = input; + Scanner(std::string input, Lox* loxRef) { + lRef = loxRef; + source = input; } std::vector<Token> scanTokens() { - return {}; + + current = 0; + + while(!isDone()) { + start = current; + scanToken(); + } + + tokens.push_back({TokenType::EOF_, "" , {} , line}); + return tokens; } private: - std::string inp; + + int start; + int current = 0; + int line = 1; + Lox* lRef; + + std::string source; + std::vector<Token> tokens; + + bool isDone() { + return current >= source.size(); + } + char advance() { + char c = source[current]; + current += 1; + return c; + } + + void addToken(TokenType type, Literal lit) { + std::string text = source.substr(start,current-start); + Token tk = Token(type, text, lit, line); + tokens.push_back(tk); + } + + void addToken(TokenType tp) { + addToken(tp, {}); + } + + void scanToken() { + char c = advance(); + switch (c) { + case '(': + addToken(TokenType::LEFT_PAREN); + break; + case ')': + addToken(TokenType::RIGHT_PAREN); + break; + case '{': + addToken(TokenType::LEFT_BRACE); + break; + case '}': + addToken(TokenType::RIGHT_BRACE); + break; + case ',': + addToken(TokenType::COMMA); + break; + case '.': + addToken(TokenType::DOT); + break; + case '-': + addToken(TokenType::MINUS); + break; + case '+': + addToken(TokenType::PLUS); + break; + case ';': + addToken(TokenType::SEMICOLON); + break; + case '*': + addToken(TokenType::STAR); + break; + default: + lRef->error(line, "Unexpected character."); + break; + } + } }; diff --git a/lox/stub.cpp b/lox/stub.cpp @@ -0,0 +1,13 @@ +#include <stdio.h> +#include <iostream> +#include <fstream> +#include <filesystem> +#include <vector> + +#include "lox.hpp" + +#include "token.cpp" +#include "scanner.cpp" + +#include "lox.cpp" +#include "main.cpp" diff --git a/lox/token.cpp b/lox/token.cpp @@ -29,6 +29,10 @@ std::string typeAsString(TokenType type) { return "LEFT_PAREN"; case TokenType::RIGHT_PAREN: return "RIGHT_PAREN"; + case TokenType::LEFT_BRACE: + return "LEFT_BRACE"; + case TokenType::RIGHT_BRACE: + return "RIGHT_BRACE"; case TokenType::COMMA: return "COMMA"; case TokenType::DOT: @@ -109,6 +113,12 @@ class Token { std::string lexeme; Literal literal; int line; + Token(TokenType tp, std::string lex, Literal li, int ln) { + type = tp; + lexeme = lex; + literal = li; + line = ln; + } std::string toString() { return typeAsString(type) + " " + lexeme + " " + literal.toString();