Lexer done for now, moving on to ast

This commit is contained in:
Michael Fabian 'Xaymar' Dirks
2024-06-26 00:31:06 +02:00
parent fa81c2a7fa
commit dfe3e88dbd
15 changed files with 642 additions and 103 deletions
+29 -1
View File
@@ -1 +1,29 @@
#include "ast.hpp"
// AUTOGENERATED COPYRIGHT HEADER START
// Copyright (C) 2017-2024 Michael Fabian 'Xaymar' Dirks <info@xaymar.com>
// AUTOGENERATED COPYRIGHT HEADER END
#include "ast.hpp"
#include <cstdlib>
blitz::ast::value_expression::value_expression(blitz::token token) : expression(token) {}
blitz::ast::integer_expression::integer_expression(blitz::token token) : value_expression(token), _value(0)
{
if (_token.text.length() > 0) {
_value = atol(_token.text.c_str());
}
}
blitz::ast::real_expression::real_expression(blitz::token token) : value_expression(token), _value(0.0f)
{
if (_token.text.length() > 0) {
_value = atof(_token.text.c_str());
}
}
blitz::ast::string_expression::string_expression(blitz::token token) : value_expression(token), _value()
{
_value = _token.text;
}
blitz::ast::variable_expression::variable_expression(blitz::token token) : expression(token) {}
+104 -3
View File
@@ -2,12 +2,113 @@
// Copyright (C) 2017-2024 Michael Fabian 'Xaymar' Dirks <info@xaymar.com>
// AUTOGENERATED COPYRIGHT HEADER END
#pragma once
#include <list>
#include <memory>
#include <optional>
#include <string>
#include "../lexer.hpp"
// BlitzBasic Built-Ins
// - Include: Followed by a String, which is the file to include at this location.
// - Const: Defines one or more constants, which can not be modified.
// - Local: Defines one or more local variables, which can be modified.
// - Global: Defines one or more global variables, which can be modified from anywhere.
// - Dim: Defines a dynamically resizable array, globally accessible and modifyable.
// - If, Then, ElseIf, EndIf: Your classic If.
// - Select, Case, Default, End Select: Your classic Select.
// - For, To, Step, Next: Your classic For loop.
// - While, Wend: Your classic While loop.
// - Repeat, Until, Forever: Similar to While/Wend, but the condition is at the end and will only loop if the condition is false.
// - Exit: Exit from any currently active loops. I guess this allows leaving Repeat/Forever combinations.
// - True, False: True and False. What, did you expect something else?
// - Stop: Pause the program for the debugger, if there happens to be one.
// - End: Terminate the program.
// - Goto: Jump to a specific label, without setting the return address.
// - Gosub, Return: Jump to a specific label (subroutine), setting the return address appropriately, and Return from it eventually.
// - Function, Return, End Function: Defines a function, and allows returning values. Yes, I know, End itself terminates the program, this is a special case. Thanks younger Sibly.
// - And, Or, Not: Logical operator, self-explanatory really.
namespace blitz {
namespace ast {
class expression {
public:
virtual ~expression() {};
virtual ~expression() = default;
};
}
}
// Values
class value_expression : public expression {
protected:
blitz::token _token;
public:
virtual ~value_expression() = default;
value_expression(blitz::token token);
};
class integer_expression : public value_expression {
protected:
int32_t _value;
public:
virtual ~integer_expression() = default;
integer_expression(blitz::token token);
};
class real_expression : public value_expression {
protected:
float _value;
public:
virtual ~real_expression() = default;
real_expression(blitz::token token);
};
class string_expression : public value_expression {
std::string _value;
public:
virtual ~string_expression() = default;
string_expression(blitz::token token);
};
/** One or more constant values
*
* Const var = Value, var2 = value
*/
class const_expression : public expression {
std::list<std::shared_ptr<variable_expression>> _values;
};
/** One or more local variables
*
* Local var, var2 = value, var3
*/
class local_expression : public expression {
std::list<std::shared_ptr<variable_expression>> _values;
};
/** One or more global variables
*
* Local var, var2 = value, var3
*/
class global_expression : public expression {
std::list<std::shared_ptr<variable_expression>> _values;
};
/** A variable definition
*
*
*/
class variable_expression : public expression {
blitz::token _assign;
std::string _name;
std::shared_ptr<value_expression> _value;
public:
virtual ~variable_expression() = default;
variable_expression(blitz::token token);
};
} // namespace ast
} // namespace blitz
+3 -3
View File
@@ -17,7 +17,7 @@ blitz::ast::NumberExpression::NumberExpression(int32_t value) : value(value) {}
blitz::ast::NumberExpression::~NumberExpression() {}
blitz::ast::ValueType blitz::ast::NumberExpression::GetType() {
return ValueType::Number;
return ValueType::INTEGER;
}
blitz::ast::DecimalExpression::DecimalExpression(float_t value) : value(value) {}
@@ -25,7 +25,7 @@ blitz::ast::DecimalExpression::DecimalExpression(float_t value) : value(value) {
blitz::ast::DecimalExpression::~DecimalExpression() {}
blitz::ast::ValueType blitz::ast::DecimalExpression::GetType() {
return ValueType::Decimal;
return ValueType::REAL;
}
blitz::ast::StringExpression::StringExpression(std::string value) : value(value) {}
@@ -33,7 +33,7 @@ blitz::ast::StringExpression::StringExpression(std::string value) : value(value)
blitz::ast::StringExpression::~StringExpression() {}
blitz::ast::ValueType blitz::ast::StringExpression::GetType() {
return ValueType::String;
return ValueType::STRING;
}
blitz::ast::ConstExpression::ConstExpression(std::string& name, std::unique_ptr<ValueExpression> value)
+7 -7
View File
@@ -13,11 +13,11 @@
namespace blitz {
namespace ast {
enum class ValueType : int8_t {
Unknown,
Number,
Decimal,
String,
Type,
UNKNOWN,
INTEGER,
REAL,
STRING,
TYPE,
};
class ValueExpression : public expression {
@@ -39,7 +39,7 @@ namespace blitz {
class VariableExpression : public ValueExpression {
public:
VariableExpression(std::string& name, ValueType type = ValueType::Number);
VariableExpression(std::string& name, ValueType type = ValueType::INTEGER);
virtual ~VariableExpression();
virtual ValueType GetType() override;
@@ -82,4 +82,4 @@ namespace blitz {
std::string value;
};
}
}
}