Initial test bootstrap.
This commit is contained in:
parent
6c618749f1
commit
4b308f6c32
94 changed files with 2543 additions and 681 deletions
78
src/base/compiler/KScope.cpp
Normal file
78
src/base/compiler/KScope.cpp
Normal file
|
@ -0,0 +1,78 @@
|
|||
#include "KScope.h"
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
void KScopeLexer::run(const FileSystemPath& path)
|
||||
{
|
||||
printf("Before read");
|
||||
File f(path);
|
||||
|
||||
const auto content = f<.readText();
|
||||
printf("Content is: %s\n", content);
|
||||
fflush(stdout);
|
||||
|
||||
Vector<Token> tokens;
|
||||
for(const auto c : content.data())
|
||||
{
|
||||
printf("Char is: %c\n", c);
|
||||
if (CharUtils::is_space(c))
|
||||
{
|
||||
if (m_working_token.is_identifier() || m_working_token.is_number())
|
||||
{
|
||||
on_token_finished(tokens);
|
||||
}
|
||||
}
|
||||
else if (CharUtils::is_alpha(c))
|
||||
{
|
||||
if (m_working_token.is_number())
|
||||
{
|
||||
on_token_finished(tokens);
|
||||
m_working_token = Token();
|
||||
m_working_token.set_is_identifier();
|
||||
}
|
||||
else if (!m_working_token.is_identifier())
|
||||
{
|
||||
m_working_token = Token();
|
||||
m_working_token.set_is_identifier();
|
||||
}
|
||||
m_working_token.m_value += c;
|
||||
}
|
||||
else if (CharUtils::is_digit(c))
|
||||
{
|
||||
if (m_working_token.is_number() || m_working_token.is_identifier())
|
||||
{
|
||||
m_working_token.m_value += c;
|
||||
}
|
||||
else
|
||||
{
|
||||
m_working_token = Token();
|
||||
m_working_token.set_is_number();
|
||||
m_working_token.m_value += c;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (m_working_token.is_identifier() || m_working_token.is_number())
|
||||
{
|
||||
on_token_finished(tokens);
|
||||
}
|
||||
|
||||
m_working_token.set_is_literal();
|
||||
m_working_token.m_value += c;
|
||||
on_token_finished(tokens);
|
||||
}
|
||||
}
|
||||
on_token_finished(tokens);
|
||||
}
|
||||
|
||||
void KScopeLexer::on_token_finished(Vector<Token>& tokens)
|
||||
{
|
||||
if (m_working_token.m_type == TokenT::NONE)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
m_working_token.on_finished();
|
||||
tokens.push_back(m_working_token);
|
||||
m_working_token = Token();
|
||||
}
|
86
src/base/compiler/KScope.h
Normal file
86
src/base/compiler/KScope.h
Normal file
|
@ -0,0 +1,86 @@
|
|||
#pragma once
|
||||
|
||||
#include "String.h"
|
||||
#include "File.h"
|
||||
#include "Vector.h"
|
||||
#include "CharUtils.h"
|
||||
|
||||
class KScopeLexer
|
||||
{
|
||||
public:
|
||||
enum class TokenT
|
||||
{
|
||||
NONE,
|
||||
DEF, // LANG COMMANDS
|
||||
EXTERN,
|
||||
IDENTIFIER ,// GENERAL
|
||||
LITERAL,
|
||||
NUMBER
|
||||
};
|
||||
|
||||
struct Token
|
||||
{
|
||||
bool is_identifier() const
|
||||
{
|
||||
return m_type == TokenT::IDENTIFIER;
|
||||
}
|
||||
|
||||
bool is_number() const
|
||||
{
|
||||
return m_type == TokenT::NUMBER;
|
||||
}
|
||||
|
||||
void set_is_identifier()
|
||||
{
|
||||
m_type = TokenT::IDENTIFIER;
|
||||
}
|
||||
|
||||
void set_is_number()
|
||||
{
|
||||
m_type = TokenT::NUMBER;
|
||||
}
|
||||
|
||||
void set_is_literal()
|
||||
{
|
||||
m_type = TokenT::LITERAL;
|
||||
}
|
||||
|
||||
void on_identifier_finished()
|
||||
{
|
||||
if (m_value == "def")
|
||||
{
|
||||
m_type = TokenT::DEF;
|
||||
}
|
||||
else if (m_value == "extern")
|
||||
{
|
||||
m_type = TokenT::EXTERN;
|
||||
}
|
||||
}
|
||||
|
||||
void on_number_finished()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void on_finished()
|
||||
{
|
||||
if (is_identifier())
|
||||
{
|
||||
on_identifier_finished();
|
||||
}
|
||||
else if(is_number())
|
||||
{
|
||||
on_number_finished();
|
||||
}
|
||||
}
|
||||
|
||||
TokenT m_type{TokenT::NONE};
|
||||
String m_value;
|
||||
};
|
||||
|
||||
void run(const FileSystemPath& path);
|
||||
|
||||
void on_token_finished(Vector<Token>& tokens);
|
||||
|
||||
Token m_working_token;
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue