From 4f8d4f378cffa71b4e80bfa2049e2152b429615c Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Thu, 25 Jul 2013 15:24:22 -0700 Subject: [PATCH] AST no templates --- fish.xcodeproj/project.pbxproj | 8 ++- parse_productions.cpp | 63 +++++++++++++++++++ ...tree_construction.h => parse_productions.h | 61 ++++++++++++++++++ parse_tree.cpp | 2 +- parse_tree.h | 4 +- 5 files changed, 133 insertions(+), 5 deletions(-) create mode 100644 parse_productions.cpp rename parse_tree_construction.h => parse_productions.h (93%) diff --git a/fish.xcodeproj/project.pbxproj b/fish.xcodeproj/project.pbxproj index 5ae10bfc4..708e65e30 100644 --- a/fish.xcodeproj/project.pbxproj +++ b/fish.xcodeproj/project.pbxproj @@ -152,6 +152,7 @@ D0F019FD15A977CA0034B3B1 /* config.fish in CopyFiles */ = {isa = PBXBuildFile; fileRef = D0C4FD9415A7D7EE00212EF1 /* config.fish */; }; D0F01A0315A978910034B3B1 /* osx_fish_launcher.m in Sources */ = {isa = PBXBuildFile; fileRef = D0D02AFA159871B2008E62BD /* osx_fish_launcher.m */; }; D0F01A0515A978A10034B3B1 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D0CBD583159EEE010024809C /* Foundation.framework */; }; + D0FE8EE8179FB760008C9F21 /* parse_productions.cpp in Sources */ = {isa = PBXBuildFile; fileRef = D0FE8EE7179FB75F008C9F21 /* parse_productions.cpp */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -465,7 +466,8 @@ D0D2693C159835CA005D9B9C /* fish */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = fish; sourceTree = BUILT_PRODUCTS_DIR; }; D0F3373A1506DE3C00ECEFC0 /* builtin_test.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = builtin_test.cpp; sourceTree = ""; }; D0F5E28415A7A32D00315DFF /* config.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = config.h; sourceTree = ""; }; - D0FE8EE6179CA8A5008C9F21 /* parse_tree_construction.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = parse_tree_construction.h; sourceTree = ""; }; + D0FE8EE6179CA8A5008C9F21 /* parse_productions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = parse_productions.h; sourceTree = ""; }; + D0FE8EE7179FB75F008C9F21 /* parse_productions.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = parse_productions.cpp; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -589,8 +591,9 @@ D0A0853C13B3ACEE0099B651 /* exec.cpp */, D0A0850C13B3ACEE0099B651 /* expand.h */, D0A0853D13B3ACEE0099B651 /* expand.cpp */, + D0FE8EE6179CA8A5008C9F21 /* parse_productions.h */, + D0FE8EE7179FB75F008C9F21 /* parse_productions.cpp */, D0C52F361765284C00BFAB82 /* parse_tree.h */, - D0FE8EE6179CA8A5008C9F21 /* parse_tree_construction.h */, D0C52F351765284C00BFAB82 /* parse_tree.cpp */, D0C52F341765281F00BFAB82 /* parse_exec.h */, D0C52F331765281F00BFAB82 /* parse_exec.cpp */, @@ -1116,6 +1119,7 @@ D0D02A89159839DF008E62BD /* fish.cpp in Sources */, D0C52F371765284C00BFAB82 /* parse_tree.cpp in Sources */, D0C52F381765720600BFAB82 /* parse_exec.cpp in Sources */, + D0FE8EE8179FB760008C9F21 /* parse_productions.cpp in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/parse_productions.cpp b/parse_productions.cpp new file mode 100644 index 000000000..82bdd0b91 --- /dev/null +++ b/parse_productions.cpp @@ -0,0 +1,63 @@ +#include "parse_productions.h" + +using namespace parse_productions; + +#define PRODUCTIONS(sym) static const Production_t sym##_productions + +PRODUCTIONS(job_list) = + { + {}, + {symbol_job, symbol_job_list}, + {parse_token_type_end, symbol_job_list} + }; + + + +/* A job_list is a list of jobs, separated by semicolons or newlines */ + +DEC(job_list) { + symbol_job_list, + { + {}, + {symbol_job, symbol_job_list}, + {parse_token_type_end, symbol_job_list} + }, + resolve_job_list +}; + +static int resolve_job_list(parse_token_type_t token_type, parse_keyword_t token_keyword) + { + switch (token_type) + { + case parse_token_type_string: + // 'end' is special + switch (token_keyword) + { + case parse_keyword_end: + case parse_keyword_else: + // End this job list + return 0; + + default: + // Normal string + return 1; + } + + case parse_token_type_pipe: + case parse_token_type_redirection: + case parse_token_type_background: + return 1; + + case parse_token_type_end: + // Empty line + return 2; + + case parse_token_type_terminate: + // no more commands, just transition to empty + return 0; + break; + + default: + return NO_PRODUCTION; + } + } \ No newline at end of file diff --git a/parse_tree_construction.h b/parse_productions.h similarity index 93% rename from parse_tree_construction.h rename to parse_productions.h index fb9e8dfbf..5ded6af00 100644 --- a/parse_tree_construction.h +++ b/parse_productions.h @@ -10,6 +10,67 @@ /* Terrifying template black magic. */ +/* + +- Get info for symbol +- Resolve production from info +- Get productions for children +- Get symbols for productions + +Production may be: + +1. Single value +2. Sequence of values (possibly empty) +3. Options of Single / Sequence + +Info to specify: + +1. Number of different productions +2. Resolver function +3. Symbols for associated productions + +Choice: should info be a class or a data? + +data: + +struct Symbol_t +{ + enum parse_token_type_t token_type; + int (*resolver)(parse_token_type_t tok, parse_keyword_t key); //may be trivial + production productions[5]; +} + +struct Production_t +{ + enum parse_token_type_t symbols[5]; +} + +*/ + +namespace parse_productions +{ + +#define MAX_PRODUCTIONS 5 +#define MAX_SYMBOLS_PER_PRODUCTION 5 + + + +struct Production_t +{ + enum parse_token_type_t symbols[MAX_SYMBOLS_PER_PRODUCTION]; +}; + +struct Symbol_t +{ + enum parse_token_type_t token_type; + int (*resolver)(parse_token_type_t tok, parse_keyword_t key); + Production_t productions[MAX_PRODUCTIONS]; +}; + + + +} + namespace parse_symbols { diff --git a/parse_tree.cpp b/parse_tree.cpp index aea3e729f..4df277d48 100644 --- a/parse_tree.cpp +++ b/parse_tree.cpp @@ -1,4 +1,4 @@ -#include "parse_tree_construction.h" +#include "parse_productions.h" #include "tokenizer.h" #include diff --git a/parse_tree.h b/parse_tree.h index 6b1fc0d19..dfe9f24e9 100644 --- a/parse_tree.h +++ b/parse_tree.h @@ -3,8 +3,8 @@ Programmatic representation of fish code. */ -#ifndef FISH_PARSE_TREE_H -#define FISH_PARSE_TREE_H +#ifndef FISH_PARSE_PRODUCTIONS_H +#define FISH_PARSE_PRODUCTIONS_H #include