Skip to content

Commit

Permalink
Separated stages
Browse files Browse the repository at this point in the history
  • Loading branch information
srp-mx committed Dec 18, 2022
1 parent bdc01d9 commit 37d5eab
Show file tree
Hide file tree
Showing 9 changed files with 624 additions and 584 deletions.
82 changes: 82 additions & 0 deletions src/ast/ast.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#pragma once
// NOTE(srp): Still not final platform-independent code

#include <memory>
#include <vector>
#include <string>
#include "../platform/typedefs/typedefs.hpp"

/// ExprAST - Base class for all expression nodes.
class ExprAST
{
// TODO(srp): Add a type field
public:
virtual ~ExprAST() {}
};

/// NumberExprAST - Expression class for numeric literals like "1.0"
class NumberExprAST : public ExprAST
{
real64 Val;

public:
NumberExprAST(double Val) : Val(Val) {}
};

/// VariableExprAST - Expression class for referencing a variable, like "a".
class VariableExprAST : public ExprAST
{
std::string Name;

public:
VariableExprAST(const std::string &Name) : Name(Name) {}
};

/// BinaryExprAST - Expression class for a binary operator.
class BinaryExprAST : public ExprAST
{
char Op;
std::unique_ptr<ExprAST> LHS, RHS;

public:
BinaryExprAST(char op, std::unique_ptr<ExprAST> LHS, std::unique_ptr<ExprAST> RHS)
: Op(op), LHS(std::move(LHS)), RHS(std::move(RHS)) {}
};

/// CallExprAST - Expression class for function calls.
class CallExprAST : public ExprAST
{
std::string Callee;
std::vector<std::unique_ptr<ExprAST>> Args;

public:
CallExprAST(const std::string & Callee, std::vector<std::unique_ptr<ExprAST>> Args)
: Callee(Callee), Args(std::move(Args)) {}
};

/// PrototypeAST - This class represents the "prototype" for a function,
/// which captures its name and its argument names (thus, the number of args too)
class PrototypeAST
{
std::string Name;
std::vector<std::string> Args;

public:
PrototypeAST(const std::string &name, std::vector<std::string> Args)
: Name(name), Args(std::move(Args)) {}

const std::string &getName() const { return Name; }
};

/// FunctionAST - This class represents a function definition itself.
class FunctionAST
{
std::unique_ptr<PrototypeAST> Proto;
std::unique_ptr<ExprAST> Body;

public:
FunctionAST(std::unique_ptr<PrototypeAST> Proto, std::unique_ptr<ExprAST> Body)
: Proto(std::move(Proto)), Body(std::move(Body)) {}
};


23 changes: 23 additions & 0 deletions src/error_handling/parser_err.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once
// NOTE(srp): Still not final platform-independent code

#include <memory>
#include "../platform/typedefs/typedefs.hpp"

#include "../ast/ast.cpp"

/// LogError* - These are little helper functions for error handling.
std::unique_ptr<ExprAST>
LogError(const char *Str)
{
fprintf(stderr, "LogError: %s\n", Str);
return nullptr;
}

std::unique_ptr<PrototypeAST>
LogErrorP(const char *Str)
{
LogError(Str);
return nullptr;
}

76 changes: 76 additions & 0 deletions src/kaleidoscope.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,79 @@
#pragma once
// NOTE(srp): Still not final platform-independent code

#include "kaleidoscope.hpp"

#include "lexer/lexer.cpp"
#include "ast/ast.cpp"
#include "parser/parser.cpp"

internal void
HandleDefinition()
{
if (ParseDefinition())
{
fprintf(stderr, "Parsed a function definition.\n");
}
else
{
// Skip token for error recovery.
getNextToken();
}

}

internal void
HandleExtern()
{
if (ParseExtern())
{
fprintf(stderr, "Parsed an extern\n");
}
else
{
// Skip token for error recovery.
getNextToken();
}
}

internal void
HandleTopLevelExpression()
{
// Evaluate a top-level expression into an anonymous function.
if (ParseTopLevelExpr())
{
fprintf(stderr, "Parsed a top-level expr\n");
}
else
{
// Skip token for error recovery.
getNextToken();
}
}

internal void
MainLoop()
{
while (1)
{
fprintf(stderr, "ready> ");
switch (CurTok)
{
case tok_eof:
return;
case ';': // ignore top-level semicolons.
getNextToken();
break;
case tok_def:
HandleDefinition();
break;
case tok_extern:
HandleExtern();
break;
default:
HandleTopLevelExpression();
break;
}
}
}

1 change: 1 addition & 0 deletions src/kaleidoscope.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include "platform/typedefs/typedefs.hpp"

// TODO(srp): Services that the platform layer provides to the program.

Expand Down
102 changes: 102 additions & 0 deletions src/lexer/lexer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#pragma once
// NOTE(srp): Still not final platform-independent code

#include <string>
#include "../platform/typedefs/typedefs.hpp"

global_variable std::string IdentifierStr; // Filled in if tok_identifier
global_variable real64 NumVal; // Filled in if tok_number

// Tokens [0-255] if it's an unknown character, otherwise one of
// the following for known things
enum Token {
tok_eof = -1,

// commands
tok_def = -2,
tok_extern = -3,

// primary
tok_identifier = -4,
tok_number = -5,
};

// gettok - Return the next token from standard input
internal int32
gettok()
{
local_persist int32 LastChar = ' ';

// Skip whitespace
while (isspace(LastChar))
{
LastChar = getchar();
}

// IDENTIFIERS
if (isalpha(LastChar))
{
// IdentifierStr: [a-zA-Z][a-zA-Z0-9]*
IdentifierStr = LastChar;
while (isalnum((LastChar = getchar())))
{
IdentifierStr += LastChar;
}

if (IdentifierStr == "def")
{
return tok_def;
}

if (IdentifierStr == "extern")
{
return tok_extern;
}

return tok_identifier;
}

// NUMBERS
if (isdigit(LastChar) || LastChar == '.')
{
// NumStr: [0-9.]+
std::string NumStr;
do
{
NumStr += LastChar;
LastChar = getchar();
} while (isdigit(LastChar) || LastChar == '.');

NumVal = strtod(NumStr.c_str(), 0);
return tok_number;

// TODO(srp): This will incorrectly read "1.23.45.67" as "1.23"
}

// COMMENTS
if (LastChar == '#')
{
// Comment until end of line
do
{
LastChar = getchar();
} while (LastChar != EOF && LastChar != '\n' && LastChar != '\r');

if (LastChar != EOF)
{
return gettok();
}
}

// Check for end of file. Don't eat the EOF.
if (LastChar == EOF)
{
return tok_eof;
}

// Otherwise, just return the character as its ascii value
int32 ThisChar = LastChar;
LastChar = getchar();
return ThisChar;
}

Loading

0 comments on commit 37d5eab

Please sign in to comment.