From 1abe80617812579ec9ff09949b766384e51e226e Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Wed, 24 Jun 2009 03:36:11 +0000 Subject: [PATCH] Added a few more rules trying to get a feeling for how this will play out --- chaioop/CMakeLists.txt | 4 +- chaioop/main.cpp | 89 +++++++++++++++++++++++++++++++++++++++--- 2 files changed, 86 insertions(+), 7 deletions(-) diff --git a/chaioop/CMakeLists.txt b/chaioop/CMakeLists.txt index 1dc9c0ac..df0e108d 100644 --- a/chaioop/CMakeLists.txt +++ b/chaioop/CMakeLists.txt @@ -3,8 +3,8 @@ cmake_minimum_required(VERSION 2.6) project(chaioop) SET (CMAKE_BUILD_TYPE gdb) -SET (CMAKE_C_FLAGS_GDB " -Wall -ggdb") -SET (CMAKE_CXX_FLAGS_GDB " -Wall -ggdb") +SET (CMAKE_C_FLAGS_GDB " -Wall -ggdb -O3") +SET (CMAKE_CXX_FLAGS_GDB " -Wall -ggdb -O3") include_directories(../langkit ../dispatchkit) diff --git a/chaioop/main.cpp b/chaioop/main.cpp index 50174336..5c23be99 100644 --- a/chaioop/main.cpp +++ b/chaioop/main.cpp @@ -1,5 +1,10 @@ #include #include +#include +#include + +#include +#include namespace langkit { class Token { @@ -46,6 +51,24 @@ namespace langkit { return retval; } + bool Str(const char *s) { + bool retval = false; + int len = strlen(s); + + if ((input_end - input_pos) >= len) { + std::string::iterator tmp = input_pos; + for (int i = 0; i < len; ++i) { + if (*tmp != s[i]) { + return false; + } + } + retval = true; + input_pos = tmp; + } + + return retval; + } + bool Arg_List() { bool retval = Id() || Int(); while (retval && Char(',')) { @@ -54,21 +77,77 @@ namespace langkit { return retval; } + bool Fun_Call() { + bool retval = false; + + std::string::iterator prev = input_pos; + + if (Id() && Char('(')) { + + Arg_List(); + retval = Char(')'); + } + + if (!retval) { input_pos = prev; } + + return retval; + } + + bool Eol() { + bool retval = false; + + if ((input_pos != input_end) && (Str("\r\n") || Char('\n'))) { + retval = true; + } + + return retval; + } + + bool Fun_Calls() { + bool retval = false; + while ((Fun_Call() && Eol()) || (Eol())) { } + + if (input_pos == input_end) { + retval = true; + } + + return retval; + } + bool parse(std::string input) { input_pos = input.begin(); input_end = input.end(); - return Id() && Char('(') && Arg_List() && Char(')'); + return Fun_Calls(); } }; }; -int main() { +std::string load_file(const char *filename) { + std::ifstream infile (filename, std::ios::in | std::ios::ate); + + if (!infile.is_open()) { + std::string fname = filename; + throw std::runtime_error("Can not open: " + fname); + } + + std::streampos size = infile.tellg(); + infile.seekg(0, std::ios::beg); + + std::vector v(size); + infile.read(&v[0], size); + + std::string ret_val (v.empty() ? std::string() : std::string (v.begin(), v.end()).c_str()); + + return ret_val; +} + +int main(int argc, char *argv[]) { langkit::Parser parser; - std::cout << "Hello, world" << std::endl; - - std::cout << parser.parse("e(x,10)") << std::endl; + if (argc > 1) { + std::cout << parser.parse(load_file(argv[1])) << std::endl; + } return 0; }