From a75baf72962ef81746fd95bcc861d7382cd7c6fd Mon Sep 17 00:00:00 2001 From: pjht Date: Sun, 9 Sep 2018 12:41:21 -0500 Subject: [PATCH] Start parser work --- cinc.xcodeproj/project.pbxproj | 6 +++ cinc/parser.c | 68 ++++++++++++++++++++++++++++++++++ cinc/parser.h | 14 +++++++ 3 files changed, 88 insertions(+) create mode 100644 cinc/parser.c create mode 100644 cinc/parser.h diff --git a/cinc.xcodeproj/project.pbxproj b/cinc.xcodeproj/project.pbxproj index a4d28fe..e854bc5 100644 --- a/cinc.xcodeproj/project.pbxproj +++ b/cinc.xcodeproj/project.pbxproj @@ -10,6 +10,7 @@ F61910612142A876003B8798 /* main.c in Sources */ = {isa = PBXBuildFile; fileRef = F61910602142A876003B8798 /* main.c */; }; F61910692142A8C5003B8798 /* tokenize.c in Sources */ = {isa = PBXBuildFile; fileRef = F61910682142A8C5003B8798 /* tokenize.c */; }; F65A954E21454B31005FCAF5 /* token.c in Sources */ = {isa = PBXBuildFile; fileRef = F65A954D21454B31005FCAF5 /* token.c */; }; + F661C333214590930021FCCE /* parser.c in Sources */ = {isa = PBXBuildFile; fileRef = F661C332214590930021FCCE /* parser.c */; }; /* End PBXBuildFile section */ /* Begin PBXCopyFilesBuildPhase section */ @@ -31,6 +32,8 @@ F61910682142A8C5003B8798 /* tokenize.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = tokenize.c; sourceTree = ""; }; F65A954C21454B21005FCAF5 /* token.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = token.h; path = ../../../../token.h; sourceTree = ""; }; F65A954D21454B31005FCAF5 /* token.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = token.c; sourceTree = ""; }; + F661C331214590930021FCCE /* parser.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = parser.h; sourceTree = ""; }; + F661C332214590930021FCCE /* parser.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = parser.c; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -68,6 +71,8 @@ F61910682142A8C5003B8798 /* tokenize.c */, F65A954D21454B31005FCAF5 /* token.c */, F65A954C21454B21005FCAF5 /* token.h */, + F661C331214590930021FCCE /* parser.h */, + F661C332214590930021FCCE /* parser.c */, ); path = cinc; sourceTree = ""; @@ -130,6 +135,7 @@ files = ( F65A954E21454B31005FCAF5 /* token.c in Sources */, F61910692142A8C5003B8798 /* tokenize.c in Sources */, + F661C333214590930021FCCE /* parser.c in Sources */, F61910612142A876003B8798 /* main.c in Sources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/cinc/parser.c b/cinc/parser.c new file mode 100644 index 0000000..942048d --- /dev/null +++ b/cinc/parser.c @@ -0,0 +1,68 @@ +// +// parser.c +// cinc +// +// Created by Peter Terpstra on 9/9/18. +// Copyright © 2018 Peter Terpstra. All rights reserved. +// + +#include "parser.h" +#include "token.h" +#include + +static Token lahead; + +static void advance() { + lahead=*(lahead.next); +} + +static void match(unsigned char type) { + if (lahead.type!=type) { + exit(1); + } + advance(); +} + +static const char* getid() { + if (lahead.type!=TYPE_IDENT) { + exit(1); + } + const char* id=lahead.val->strval; + advance(); + return id; +} + +static int get_num() { + if (lahead.type!=TYPE_NUM) { + exit(1); + } + int num=lahead.val->intval; + advance(); + return num; +} + +static const char* gettype() { + if (lahead.type!=TYPE_TYPE) { + exit(1); + } + const char* id=lahead.val->strval; + advance(); + return id; +} + +static char** func() { + const char* type=gettype(); + const char* name=getid(); + match('('); + match(')'); + match('{'); + //block code here + match('}'); + //return ast here +} + +char** parse(Token* prg) { + lahead=*(prg); + return func(); +} + diff --git a/cinc/parser.h b/cinc/parser.h new file mode 100644 index 0000000..10a0764 --- /dev/null +++ b/cinc/parser.h @@ -0,0 +1,14 @@ +// +// parser.h +// cinc +// +// Created by Peter Terpstra on 9/9/18. +// Copyright © 2018 Peter Terpstra. All rights reserved. +// + +#ifndef parser_h +#define parser_h + +#include + +#endif /* parser_h */