Skip to content

Commit

Permalink
Variables are now exported
Browse files Browse the repository at this point in the history
  • Loading branch information
lukedupin committed Jan 24, 2020
1 parent 042b6ec commit b5c82f7
Show file tree
Hide file tree
Showing 11 changed files with 184 additions and 16 deletions.
4 changes: 4 additions & 0 deletions C--.pro
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ MOC_DIR = moc
SOURCES += \
Helpers/util.cpp \
Helpers/token.cpp \
Nodes/constant_node.cpp \
Nodes/error.cpp \
Nodes/expression_node.cpp \
Nodes/node.cpp \
Nodes/program_node.cpp \
Parser/lex.yy.c \
Expand All @@ -39,7 +41,9 @@ SOURCES += \
HEADERS += \
Helpers/util.h \
Helpers/token.h \
Nodes/constant_node.h \
Nodes/error.h \
Nodes/expression_node.h \
Nodes/node.h \
Nodes/program_node.h \
Parser/lex_token.h \
Expand Down
3 changes: 3 additions & 0 deletions Helpers/token.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,9 @@ const char* tokenStr( int token )
case F64:
return "F64";

case BOOL:
return "BOOL";

case STR:
return "STR";

Expand Down
28 changes: 28 additions & 0 deletions Nodes/constant_node.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include "constant_node.h"

#include <Parser/lex_token.h>
#include "parser.tab.h"

ConstantNode::ConstantNode( int code, int line, QString value, int type_code, int base ) :
Node( code, line, value ),
_typeCode( type_code ),
_base( base )
{
if ( type_code < 0 )
{
if ( value.contains('.'))
_typeCode = F64;
else
_typeCode = I32;
}
}

int ConstantNode::typeCode()
{
return _typeCode;
}

int ConstantNode::base()
{
return _base;
}
20 changes: 20 additions & 0 deletions Nodes/constant_node.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef CONSTANTNODE_H
#define CONSTANTNODE_H

#include "node.h"

class ConstantNode : public Node
{
private:
int _typeCode = -1;
int _base = 10;

public:
ConstantNode( int code, int line, QString value, int type_code = -1, int base = 10 );

int typeCode();

int base();
};

#endif // CONSTANTNODE_H
12 changes: 9 additions & 3 deletions Nodes/declare_variable.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "declare_variable.h"

DeclareVariable::DeclareVariable( int code, int line, char* name ) :
#include "expression_node.h"

DeclareVariable::DeclareVariable( int code, int line, QString name ) :
Node( code, line, name )
{
}
Expand All @@ -10,9 +12,13 @@ bool DeclareVariable::codeGenPreChild( QTextStream* stream, Context* context )
if ( Children.count() < 1 )
return false;

auto expression = dynamic_cast<ExpressionNode*>(Children[0]);
auto expression = dynamic_cast<ExpressionNode*>(Children.last());

QString pad;
pad.resize( context->Depth * 4, ' ');
(*stream) << << " " << name
(*stream) << expression->getTypeName( context ) << " "
<< _label << " = "
<< expression->label() << ";\r\n";

return true;
}
2 changes: 1 addition & 1 deletion Nodes/declare_variable.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
class DeclareVariable : public Node
{
public:
DeclareVariable( int code, int line, char* name );
DeclareVariable( int code, int line, QString name );

bool codeGenPreChild( QTextStream* stream, Context* context ) override;
};
Expand Down
66 changes: 66 additions & 0 deletions Nodes/expression_node.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include "expression_node.h"

#include "constant_node.h"

#include <Parser/lex_token.h>
#include "parser.tab.h"

ExpressionNode::ExpressionNode( int code, int line, QString label ) :
Node( code, line, label ),
_typeName("Unknown")
{
}

QString ExpressionNode::getTypeName( Context* context )
{
if ( !_calculated && Children.count() > 0 )
calculateType( context, Children.last() );
_calculated = true;

return _typeName;
}

int ExpressionNode::getTypeCode( Context* context )
{
if ( !_calculated )
calculateType( context, Children.last() );
_calculated = true;

return _typeCode;
}

void ExpressionNode::calculateType( Context* context, Node* node )
{
if ( node->tokenType() == IDENT)
{
}
else
{
auto cn = dynamic_cast<ConstantNode*>(node);
_typeCode = cn->typeCode();
switch ( _typeCode )
{
case I8: _typeName = "char"; break;
case I16: _typeName = "short"; break;
case I32: _typeName = "int"; break;
case I64: _typeName = "long"; break;
case I128: _typeName = "long long"; break;

case U8: _typeName = "unsigned char"; break;
case U16: _typeName = "unsigned short"; break;
case U32: _typeName = "unsigned int"; break;
case U64: _typeName = "unsigned long"; break;
case U128: _typeName = "unsigned long long"; break;

case F32: _typeName = "float"; break;
case F64: _typeName = "double"; break;

case BOOL: _typeName = "bool"; break;
case STR: _typeName = "QString"; break;

default:
_typeName = "Error";
break;
}
}
}
25 changes: 25 additions & 0 deletions Nodes/expression_node.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifndef EXPRESSIONNODE_H
#define EXPRESSIONNODE_H

#include "node.h"

#include <QString>

class ExpressionNode : public Node
{
private:
bool _calculated = false;
int _typeCode = -1;
QString _typeName;

public:
ExpressionNode( int code, int line, QString label );

QString getTypeName( Context* context );

int getTypeCode( Context* context );

void calculateType( Context* context, Node* node );
};

#endif // EXPRESSIONNODE_H
30 changes: 18 additions & 12 deletions Parser/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include <node.h>
#include <program_node.h>
#include <declare_variable.h>
#include <expression_node.h>
#include <constant_node.h>

#ifdef CPLUSPLUS
extern int yylex();
Expand Down Expand Up @@ -96,6 +98,7 @@
%token <tokInfo> U128
%token <tokInfo> F32
%token <tokInfo> F64
%token <tokInfo> BOOL
%token <tokInfo> STR
%token <tokInfo> VEC
%token <tokInfo> HASH
Expand Down Expand Up @@ -437,41 +440,44 @@ expression_list : expression ',' expression_list
expression : var ASSIGN expression
{
//Create my node
$$ = new Node( $2->code, $1->lineNumber(), $2->stringValue );
$$ = new ExpressionNode( $2->code, $1->lineNumber(), $2->stringValue );
$$->Children.push_back( $1);
$$->Children.push_back( $3);
}

| var ADD_ASSIGN expression
{
$$ = new Node( $2->code, $1->lineNumber(), $2->stringValue );
$$ = new ExpressionNode( $2->code, $1->lineNumber(), $2->stringValue );
$$->Children.push_back( $1);
$$->Children.push_back( $3);
}

| var SUB_ASSIGN expression
{
$$ = new Node( $2->code, $1->lineNumber(), $2->stringValue );
$$ = new ExpressionNode( $2->code, $1->lineNumber(), $2->stringValue );
$$->Children.push_back( $1);
$$->Children.push_back( $3);
}

| var MUL_ASSIGN expression
{
$$ = new Node( $2->code, $1->lineNumber(), $2->stringValue );
$$ = new ExpressionNode( $2->code, $1->lineNumber(), $2->stringValue );
$$->Children.push_back( $1);
$$->Children.push_back( $3);
}

| var DIV_ASSIGN expression
{
$$ = new Node( $2->code, $1->lineNumber(), $2->stringValue );
$$ = new ExpressionNode( $2->code, $1->lineNumber(), $2->stringValue );
$$->Children.push_back( $1);
$$->Children.push_back( $3);
}

| simpexp
{ $$ = $1; }
{
$$ = new ExpressionNode( $1->tokenType(), $1->lineNumber(), $1->label() );
$$->Children.push_back( $1 );
}
;

simpexp : simpexp logop relexp
Expand Down Expand Up @@ -661,19 +667,19 @@ var : IDENT

constant : NUMBER
{
$$ = new Node( NUMBER, $1->line, $1->stringValue );
$$ = new ConstantNode( NUMBER, $1->line, $1->stringValue );
}
| NUMBER ':' primative_type
{
$$ = new Node( NUMBER, $1->line, $1->stringValue );
$$ = new ConstantNode( NUMBER, $1->line, $1->stringValue, $primative_type->code );
}
| TRUE
{
$$ = new Node( $1->code, $1->line, $1->stringValue );
$$ = new ConstantNode( $1->code, $1->line, $1->stringValue, BOOL );
}
| FALSE
{
$$ = new Node( $1->code, $1->line, $1->stringValue );
$$ = new ConstantNode( $1->code, $1->line, $1->stringValue, BOOL );
}
;

Expand Down Expand Up @@ -701,11 +707,11 @@ str_params : simpexp ',' str_params

str_literal : STRING_DBL
{
$$ = new Node( $1->code, $1->line, $1->stringValue );
$$ = new ConstantNode( $1->code, $1->line, $1->stringValue, STR );
}
| STRING_TICK
{
$$ = new Node( $1->code, $1->line, $1->stringValue );
$$ = new ConstantNode( $1->code, $1->line, $1->stringValue, STR );
}
;

Expand Down
9 changes: 9 additions & 0 deletions Parser/scanner.lex
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,15 @@
s->stringValue = "f64";
return F64;
}
"bool" {
lineNo = yylineno;
LexToken * s = new LexToken;
yylval.tokInfo = s;
s->code = BOOL;
s->line = yylineno;
s->stringValue = "bool";
return BOOL;
}
"str" {
lineNo = yylineno;
LexToken * s = new LexToken;
Expand Down
1 change: 1 addition & 0 deletions Scripts/scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ def data():
{"rule": "U128"},
{"rule": "F32"},
{"rule": "F64"},
{"rule": "BOOL"},
{"rule": "STR"},
{"rule": "VEC"},
{"rule": "HASH"},
Expand Down

0 comments on commit b5c82f7

Please sign in to comment.