Progress!

This commit is contained in:
Markus Hauschild
2013-05-31 23:51:44 +02:00
parent 713b5078cd
commit 28ca85b94e
9 changed files with 129 additions and 17 deletions

View File

@@ -18,7 +18,7 @@ INCLUDE_DIRECTORIES (${CMAKE_SOURCE_DIR}/inc)
ADD_DEFINITIONS("-std=c++0x")
ADD_EXECUTABLE (lemon grammar/lemon.c)
#ADD_EXECUTABLE (makeheaders grammar/makeheaders.c)
ADD_EXECUTABLE (makeheaders grammar/makeheaders.c)
ADD_CUSTOM_COMMAND(
COMMAND cp
@@ -26,18 +26,18 @@ ADD_CUSTOM_COMMAND(
COMMAND cp
ARGS ${CMAKE_CURRENT_SOURCE_DIR}/grammar/lempar.c ${CMAKE_CURRENT_BINARY_DIR}
COMMAND ${CMAKE_CURRENT_BINARY_DIR}/lemon
# ARGS -q -m ${CMAKE_CURRENT_BINARY_DIR}/grammar.y
ARGS -q ${CMAKE_CURRENT_BINARY_DIR}/grammar.y
ARGS -q -m ${CMAKE_CURRENT_BINARY_DIR}/grammar.y
# ARGS -q ${CMAKE_CURRENT_BINARY_DIR}/grammar.y
COMMAND rm
ARGS ${CMAKE_CURRENT_BINARY_DIR}/lempar.c
COMMAND rm
ARGS ${CMAKE_CURRENT_BINARY_DIR}/grammar.y
# COMMAND ${CMAKE_CURRENT_BINARY_DIR}/makeheaders
# ARGS ${CMAKE_CURRENT_BINARY_DIR}/grammar.c
COMMAND ${CMAKE_CURRENT_BINARY_DIR}/makeheaders
ARGS ${CMAKE_CURRENT_BINARY_DIR}/grammar.c
COMMAND mv
ARGS ${CMAKE_CURRENT_BINARY_DIR}/grammar.c ${CMAKE_CURRENT_BINARY_DIR}/grammar.cpp
DEPENDS lemon
# DEPENDS makeheaders
DEPENDS makeheaders
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/grammar/grammar.y
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/grammar.cpp
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/grammar.h
@@ -56,6 +56,11 @@ ENDIF (SCULLY_DEBUG)
SET(SCULLY_SOURCE
src/test.cpp
src/Token.cpp
src/AST/ASTElement.cpp
src/AST/ASTVisitor.cpp
${CMAKE_CURRENT_BINARY_DIR}/grammar.cpp
)

View File

@@ -5,9 +5,15 @@
#include <string>
#include <assert.h>
#include "Token.h"
#include "AST/ASTElement.h"
}
%token_type {int}
%name scullyParser
%token_type {Token*}
// whitespace and comments
%type T_WHITESPACE {int}
@@ -19,40 +25,53 @@
%left T_TIMES T_DIV.
%syntax_error {
std::cout << "Syntax error!" << std::endl;
std::cerr << "Syntax error!" << std::endl;
}
programm(A) ::= fundefs(B). { A = B; }
%parse_failure {
std::cerr << "Parser Failed!" << std::endl;
}
%type program {int}
program ::= fundefs(F). { std::cout << F << std::endl; }
program ::= expr(E). { std::cout << E << std::endl; }
%type fundefs {int}
fundefs(A) ::= . { A = 0; }
fundefs(A) ::= fundefs fundef(B). { A = A + B; }
fundef(A) ::= type(T) T_IDENTIFIER(ID) params(P) T_BEGIN statements(S) T_END. { A = T + ID + P + S; }
%type fundef {int}
fundef(A) ::= type(T) T_IDENTIFIER(ID) params(P) T_BEGIN statements(S) T_END. { A = T + 1 + P + S; /* ID */ }
%type type {int}
type(A) ::= T_BOOL. { A = 1; }
type(A) ::= T_INT. { A = 1; }
type(A) ::= T_STRING. { A = 1; }
type(A) ::= T_VOID. { A = 1; }
%type params {int}
params(A) ::= . { A = 0; }
params(A) ::= type(T) T_IDENTIFIER(ID). { A = T + ID; }
params(A) ::= params(B) T_COMMA type(T) T_IDENTIFIER(ID). { A = B + T + ID; }
params(A) ::= type(T) T_IDENTIFIER(ID). { A = T + 1; /* ID */ }
params(A) ::= params(B) T_COMMA type(T) T_IDENTIFIER(ID). { A = B + T + 1; /* ID */ }
%type statement {int}
statement(A) ::= T_IF T_LPAREN expr(E) T_RPAREN statement(S). { A = E + S; }
statement(A) ::= T_RIF T_LPAREN T_CINT(P) T_RPAREN statement(S). { A = P + S; }
statement(A) ::= T_RIF T_LPAREN T_CINT(P) T_RPAREN statement(S). { A = 1 + S; /* P */ }
statement(A) ::= T_FOR T_LPAREN expr(INIT) T_SEMICOLON expr(COND) T_SEMICOLON expr(STEP) T_RPAREN statement(S).
{ A = INIT + COND + STEP + S; }
statement(A) ::= T_RFOR T_LPAREN expr(INIT) T_SEMICOLON T_CINT(P) T_SEMICOLON expr(STEP) T_RPAREN statement(S).
{ A = INIT + P + STEP + S; }
{ A = INIT + 1 + STEP + S; /* P */ }
statement(A) ::= T_RETURN expr(E) T_SEMICOLON. { A = E; }
statement(A) ::= T_BEGIN statements(S) T_END. { A = S; }
statement(A) ::= vardef(V) T_SEMICOLON. { A = V; }
statement(A) ::= expr(E) T_SEMICOLON. { A = E; }
%type statements {int}
statements(A) ::= . { A = 0; }
statements(A) ::= statements(B) statement(C). { A = B + C; }
expr(A) ::= T_IDENTIFIER(ID) T_ASSIGN expr(E). { A = ID + E; }
%type expr {int}
expr(A) ::= T_IDENTIFIER(ID) T_ASSIGN expr(E). { A = 1 + E; /* ID */ }
expr(A) ::= expr(B) T_EQUALS expr(C). { A = B + C; }
expr(A) ::= expr(B) T_LESS expr(C). { A = B + C; }
expr(A) ::= expr(B) T_PLUS expr(C). { A = B + C; }
@@ -62,10 +81,12 @@ expr(A) ::= expr(B) T_DIV expr(C). { A = B + C; }
expr(A) ::= T_CINT. { A = 1; }
expr(A) ::= T_TRUE. { A = 1; }
expr(A) ::= T_FALSE. { A = 1; }
expr(A) ::= T_IDENTIFIER(ID) T_LPAREN values(V) T_RPAREN. { A = ID + V; }
expr(A) ::= T_IDENTIFIER(ID) T_LPAREN values(V) T_RPAREN. { A = 1 + V; /* ID */ }
vardef(A) ::= type(T) T_IDENTIFIER(ID). { A = T + ID; }
%type vardef {int}
vardef(A) ::= type(T) T_IDENTIFIER(ID). { A = T + 1; /* ID */ }
%type values {int}
values(A) ::= . { A = 0; }
values(A) ::= expr(E). { A = E; }
values(A) ::= values(B) T_COMMA expr(E). { A = B + E; }

14
inc/AST/ASTElement.h Normal file
View File

@@ -0,0 +1,14 @@
#ifndef ASTELEMENT_H
#define ASTELEMENT_H
#include "AST/ASTVisitor.h"
class ASTElement {
public:
ASTElement();
virtual ~ASTElement();
virtual void accept(ASTVisitor* visitor) = 0;
};
#endif // ASTELEMENT_H

10
inc/AST/ASTVisitor.h Normal file
View File

@@ -0,0 +1,10 @@
#ifndef ASTVISITOR_H
#define ASTVISITOR_H
class ASTVisitor {
public:
ASTVisitor();
virtual ~ASTVisitor();
};
#endif // ASTVISITOR_H

20
inc/Token.h Normal file
View File

@@ -0,0 +1,20 @@
/* The scully programming language.
*
* Copyright (c) Peter Dahlberg, Markus Hauschild and Florian Sattler, 2013.
* Licensed under the GNU GPL v2.
*/
#ifndef TOKEN_H
#define TOKEN_H
#include <string>
class Token {
public:
Token(std::string text);
std::string getText();
private:
std::string text_;
};
#endif // TOKEN_H

10
src/AST/ASTElement.cpp Normal file
View File

@@ -0,0 +1,10 @@
#include "AST/ASTElement.h"
#include "AST/ASTVisitor.h"
ASTElement::ASTElement() {
//
}
ASTElement::~ASTElement() {
//
}

10
src/AST/ASTVisitor.cpp Normal file
View File

@@ -0,0 +1,10 @@
#include "AST/ASTVisitor.h"
ASTVisitor::ASTVisitor() {
//
}
ASTVisitor::~ASTVisitor() {
//
}

15
src/Token.cpp Normal file
View File

@@ -0,0 +1,15 @@
/* The scully programming language.
*
* Copyright (c) Peter Dahlberg, Markus Hauschild and Florian Sattler, 2013.
* Licensed under the GNU GPL v2.
*/
#include "Token.h"
Token::Token(std::string text) : text_(text) {
//
}
std::string Token::getText() {
return text_;
}

View File

@@ -4,6 +4,8 @@
* Licensed under the GNU GPL v2.
*/
#include "Token.h"
// this file is auto generated from grammar/grammar.y
#include "grammar.h"
@@ -59,6 +61,8 @@ int main() {
std::cout << "The scully programming languae v0.1" << std::endl;
void* parser = scullyParserAlloc(malloc);
while (true) {
std::cout << "> ";
std::string input;
@@ -77,8 +81,11 @@ int main() {
std::string s(results.start, results.end);
if (results.id != T_WHITESPACE) {
std::cout << "Id: " << results.id << ", Token: " << s << std::endl;
scullyParser(parser, results.id, new Token(s));
}
} while (results.id != 0);
}
scullyParserFree(parser, free);
return 0;
}