Start parser work

This commit is contained in:
pjht 2018-09-09 12:41:21 -05:00
parent ec99ce1465
commit a75baf7296
3 changed files with 88 additions and 0 deletions

View File

@ -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 = "<group>"; };
F65A954C21454B21005FCAF5 /* token.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = token.h; path = ../../../../token.h; sourceTree = "<group>"; };
F65A954D21454B31005FCAF5 /* token.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = token.c; sourceTree = "<group>"; };
F661C331214590930021FCCE /* parser.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = parser.h; sourceTree = "<group>"; };
F661C332214590930021FCCE /* parser.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = parser.c; sourceTree = "<group>"; };
/* 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 = "<group>";
@ -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;

68
cinc/parser.c Normal file
View File

@ -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 <stdlib.h>
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();
}

14
cinc/parser.h Normal file
View File

@ -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 <stdio.h>
#endif /* parser_h */