- Add function declarations, calls, and return statements - Implement lexical scoping with Environment class and closures - Convert print from statement to standard library function - Add assert() function to standard library for testing - Add time() function for microsecond precision benchmarking - Create StdLib class and BuiltinFunction wrapper for standard library - Implement first-class functions and higher-order functions - Add function parameter support (tested up to 100 parameters) - Support alphanumeric identifiers in variable and function names - Add underscore support in variable names and identifiers - Implement string + number and number + string concatenation - Add boolean + string and string + boolean concatenation - Support string multiplication (string * number) - Fix decimal truncation issue by using std::stod for all number parsing - Add comprehensive number formatting with proper precision handling - Support huge numbers (epoch timestamps) without integer overflow - Clean number display (no trailing zeros on integers) - Add basic error handling with program termination on errors - Add comprehensive test suite covering all features - Add escape sequence support (\n, \t, \", \\) - Add comprehensive documentation and language reference - Update development roadmap with completed features
60 lines
1.3 KiB
C++
60 lines
1.3 KiB
C++
#pragma once
|
|
#include <string>
|
|
#include <iostream>
|
|
#include <vector>
|
|
#include <memory>
|
|
#include <functional>
|
|
struct Object
|
|
{
|
|
virtual ~Object(){};
|
|
};
|
|
|
|
struct Number : Object
|
|
{
|
|
double value;
|
|
explicit Number(double value) : value(value) {}
|
|
};
|
|
|
|
struct String : Object
|
|
{
|
|
std::string value;
|
|
explicit String(std::string str) : value(str) {}
|
|
~String(){
|
|
|
|
}
|
|
};
|
|
|
|
struct Boolean : Object
|
|
{
|
|
bool value;
|
|
explicit Boolean(bool value) : value(value) {}
|
|
};
|
|
|
|
struct None : public Object
|
|
{
|
|
|
|
};
|
|
|
|
struct Function : public Object
|
|
{
|
|
const std::string name;
|
|
const std::vector<std::string> params;
|
|
const std::vector<std::shared_ptr<void>> body; // Will cast to Stmt* when needed
|
|
const std::shared_ptr<void> closure; // Will cast to Environment* when needed
|
|
|
|
Function(std::string name, std::vector<std::string> params,
|
|
std::vector<std::shared_ptr<void>> body,
|
|
std::shared_ptr<void> closure)
|
|
: name(name), params(params), body(body), closure(closure) {}
|
|
};
|
|
|
|
struct BuiltinFunction : public Object
|
|
{
|
|
const std::string name;
|
|
const std::function<std::shared_ptr<Object>(std::vector<std::shared_ptr<Object> >)> func;
|
|
|
|
BuiltinFunction(std::string name, std::function<std::shared_ptr<Object>(std::vector<std::shared_ptr<Object> >)> func)
|
|
: name(name), func(func) {}
|
|
};
|
|
|