mirror of
https://github.com/ChaiScript/ChaiScript.git
synced 2025-12-07 01:06:54 +08:00
Added a few more rules trying to get a feeling for how this will play out
This commit is contained in:
parent
f4efd62e65
commit
1abe806178
@ -3,8 +3,8 @@ cmake_minimum_required(VERSION 2.6)
|
|||||||
project(chaioop)
|
project(chaioop)
|
||||||
|
|
||||||
SET (CMAKE_BUILD_TYPE gdb)
|
SET (CMAKE_BUILD_TYPE gdb)
|
||||||
SET (CMAKE_C_FLAGS_GDB " -Wall -ggdb")
|
SET (CMAKE_C_FLAGS_GDB " -Wall -ggdb -O3")
|
||||||
SET (CMAKE_CXX_FLAGS_GDB " -Wall -ggdb")
|
SET (CMAKE_CXX_FLAGS_GDB " -Wall -ggdb -O3")
|
||||||
|
|
||||||
include_directories(../langkit ../dispatchkit)
|
include_directories(../langkit ../dispatchkit)
|
||||||
|
|
||||||
|
|||||||
@ -1,5 +1,10 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <fstream>
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
namespace langkit {
|
namespace langkit {
|
||||||
class Token {
|
class Token {
|
||||||
@ -46,6 +51,24 @@ namespace langkit {
|
|||||||
return retval;
|
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 Arg_List() {
|
||||||
bool retval = Id() || Int();
|
bool retval = Id() || Int();
|
||||||
while (retval && Char(',')) {
|
while (retval && Char(',')) {
|
||||||
@ -54,21 +77,77 @@ namespace langkit {
|
|||||||
return retval;
|
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) {
|
bool parse(std::string input) {
|
||||||
input_pos = input.begin();
|
input_pos = input.begin();
|
||||||
input_end = input.end();
|
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<char> 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;
|
langkit::Parser parser;
|
||||||
|
|
||||||
std::cout << "Hello, world" << std::endl;
|
if (argc > 1) {
|
||||||
|
std::cout << parser.parse(load_file(argv[1])) << std::endl;
|
||||||
std::cout << parser.parse("e(x,10)") << std::endl;
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user