commit 2cb008cf025654c95f24b10c1b6983332ad82765 Author: pjht Date: Sun Sep 9 12:15:27 2018 -0500 Initial Commit diff --git a/cinc.xcodeproj/project.pbxproj b/cinc.xcodeproj/project.pbxproj new file mode 100644 index 0000000..a4d28fe --- /dev/null +++ b/cinc.xcodeproj/project.pbxproj @@ -0,0 +1,289 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 50; + objects = { + +/* Begin PBXBuildFile section */ + 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 */; }; +/* End PBXBuildFile section */ + +/* Begin PBXCopyFilesBuildPhase section */ + F619105B2142A876003B8798 /* CopyFiles */ = { + isa = PBXCopyFilesBuildPhase; + buildActionMask = 2147483647; + dstPath = /usr/share/man/man1/; + dstSubfolderSpec = 0; + files = ( + ); + runOnlyForDeploymentPostprocessing = 1; + }; +/* End PBXCopyFilesBuildPhase section */ + +/* Begin PBXFileReference section */ + F619105D2142A876003B8798 /* cinc */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = cinc; sourceTree = BUILT_PRODUCTS_DIR; }; + F61910602142A876003B8798 /* main.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = main.c; sourceTree = ""; }; + F61910672142A8C5003B8798 /* tokenize.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = tokenize.h; sourceTree = ""; }; + 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 = ""; }; +/* End PBXFileReference section */ + +/* Begin PBXFrameworksBuildPhase section */ + F619105A2142A876003B8798 /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXFrameworksBuildPhase section */ + +/* Begin PBXGroup section */ + F61910542142A876003B8798 = { + isa = PBXGroup; + children = ( + F619105F2142A876003B8798 /* cinc */, + F619105E2142A876003B8798 /* Products */, + ); + sourceTree = ""; + }; + F619105E2142A876003B8798 /* Products */ = { + isa = PBXGroup; + children = ( + F619105D2142A876003B8798 /* cinc */, + ); + name = Products; + sourceTree = ""; + }; + F619105F2142A876003B8798 /* cinc */ = { + isa = PBXGroup; + children = ( + F61910602142A876003B8798 /* main.c */, + F61910672142A8C5003B8798 /* tokenize.h */, + F61910682142A8C5003B8798 /* tokenize.c */, + F65A954D21454B31005FCAF5 /* token.c */, + F65A954C21454B21005FCAF5 /* token.h */, + ); + path = cinc; + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXNativeTarget section */ + F619105C2142A876003B8798 /* cinc */ = { + isa = PBXNativeTarget; + buildConfigurationList = F61910642142A876003B8798 /* Build configuration list for PBXNativeTarget "cinc" */; + buildPhases = ( + F61910592142A876003B8798 /* Sources */, + F619105A2142A876003B8798 /* Frameworks */, + F619105B2142A876003B8798 /* CopyFiles */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = cinc; + productName = cinc; + productReference = F619105D2142A876003B8798 /* cinc */; + productType = "com.apple.product-type.tool"; + }; +/* End PBXNativeTarget section */ + +/* Begin PBXProject section */ + F61910552142A876003B8798 /* Project object */ = { + isa = PBXProject; + attributes = { + LastUpgradeCheck = 0940; + ORGANIZATIONNAME = "Peter Terpstra"; + TargetAttributes = { + F619105C2142A876003B8798 = { + CreatedOnToolsVersion = 9.4.1; + }; + }; + }; + buildConfigurationList = F61910582142A876003B8798 /* Build configuration list for PBXProject "cinc" */; + compatibilityVersion = "Xcode 9.3"; + developmentRegion = en; + hasScannedForEncodings = 0; + knownRegions = ( + en, + ); + mainGroup = F61910542142A876003B8798; + productRefGroup = F619105E2142A876003B8798 /* Products */; + projectDirPath = ""; + projectRoot = ""; + targets = ( + F619105C2142A876003B8798 /* cinc */, + ); + }; +/* End PBXProject section */ + +/* Begin PBXSourcesBuildPhase section */ + F61910592142A876003B8798 /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + F65A954E21454B31005FCAF5 /* token.c in Sources */, + F61910692142A8C5003B8798 /* tokenize.c in Sources */, + F61910612142A876003B8798 /* main.c in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXSourcesBuildPhase section */ + +/* Begin XCBuildConfiguration section */ + F61910622142A876003B8798 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_ENABLE_OBJC_WEAK = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + CODE_SIGN_IDENTITY = "-"; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = dwarf; + ENABLE_STRICT_OBJC_MSGSEND = YES; + ENABLE_TESTABILITY = YES; + GCC_C_LANGUAGE_STANDARD = gnu11; + GCC_DYNAMIC_NO_PIC = NO; + GCC_NO_COMMON_BLOCKS = YES; + GCC_OPTIMIZATION_LEVEL = 0; + GCC_PREPROCESSOR_DEFINITIONS = ( + "DEBUG=1", + "$(inherited)", + ); + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + MACOSX_DEPLOYMENT_TARGET = 10.13; + MTL_ENABLE_DEBUG_INFO = YES; + ONLY_ACTIVE_ARCH = YES; + SDKROOT = macosx; + }; + name = Debug; + }; + F61910632142A876003B8798 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_ENABLE_OBJC_WEAK = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + CODE_SIGN_IDENTITY = "-"; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; + ENABLE_NS_ASSERTIONS = NO; + ENABLE_STRICT_OBJC_MSGSEND = YES; + GCC_C_LANGUAGE_STANDARD = gnu11; + GCC_NO_COMMON_BLOCKS = YES; + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + MACOSX_DEPLOYMENT_TARGET = 10.13; + MTL_ENABLE_DEBUG_INFO = NO; + SDKROOT = macosx; + }; + name = Release; + }; + F61910652142A876003B8798 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + CODE_SIGN_STYLE = Automatic; + PRODUCT_NAME = "$(TARGET_NAME)"; + }; + name = Debug; + }; + F61910662142A876003B8798 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + CODE_SIGN_STYLE = Automatic; + PRODUCT_NAME = "$(TARGET_NAME)"; + }; + name = Release; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + F61910582142A876003B8798 /* Build configuration list for PBXProject "cinc" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + F61910622142A876003B8798 /* Debug */, + F61910632142A876003B8798 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + F61910642142A876003B8798 /* Build configuration list for PBXNativeTarget "cinc" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + F61910652142A876003B8798 /* Debug */, + F61910662142A876003B8798 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; +/* End XCConfigurationList section */ + }; + rootObject = F61910552142A876003B8798 /* Project object */; +} diff --git a/cinc.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/cinc.xcodeproj/project.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..8a4b50a --- /dev/null +++ b/cinc.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/cinc.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist b/cinc.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist new file mode 100644 index 0000000..18d9810 --- /dev/null +++ b/cinc.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist @@ -0,0 +1,8 @@ + + + + + IDEDidComputeMac32BitWarning + + + diff --git a/cinc.xcodeproj/xcuserdata/peterterpstra.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist b/cinc.xcodeproj/xcuserdata/peterterpstra.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist new file mode 100644 index 0000000..fe2b454 --- /dev/null +++ b/cinc.xcodeproj/xcuserdata/peterterpstra.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist @@ -0,0 +1,5 @@ + + + diff --git a/cinc.xcodeproj/xcuserdata/peterterpstra.xcuserdatad/xcschemes/xcschememanagement.plist b/cinc.xcodeproj/xcuserdata/peterterpstra.xcuserdatad/xcschemes/xcschememanagement.plist new file mode 100644 index 0000000..e10c83c --- /dev/null +++ b/cinc.xcodeproj/xcuserdata/peterterpstra.xcuserdatad/xcschemes/xcschememanagement.plist @@ -0,0 +1,14 @@ + + + + + SchemeUserState + + cinc.xcscheme + + orderHint + 0 + + + + diff --git a/cinc/main.c b/cinc/main.c new file mode 100644 index 0000000..ab7ff94 --- /dev/null +++ b/cinc/main.c @@ -0,0 +1,28 @@ +// +// main.c +// cinc +// +// Created by Peter Terpstra on 9/7/18. +// Copyright © 2018 Peter Terpstra. All rights reserved. +// + +#include +#include +#include +#include "tokenize.h" + +Token* tokenfunc(void); + +int main(int argc, const char * argv[]) { + char* prgstr="int main() {\nreturn 0;\n}"; + Token* tokens=tokenize(prgstr); + Token* tok=tokens; + while (tok) { + print_tok(tok); + if (tok->type==TYPE_EOF) { + break; + } + tok=tok->next; + } + free_toklist(tokens); +} diff --git a/cinc/token.c b/cinc/token.c new file mode 100644 index 0000000..5183a94 --- /dev/null +++ b/cinc/token.c @@ -0,0 +1,85 @@ +// +// token.c +// cinc +// +// Created by Peter Terpstra on 9/7/18. +// Copyright © 2018 Peter Terpstra. All rights reserved. +// + +#include "token.h" +#include +#include + +Token* new_token(unsigned char type,TokenVal* val, Token* prev) { + Token* token=malloc(sizeof(Token)); + token->type=type; + token->val=val; + token->next=NULL; + if (prev) { + prev->next=token; + } + return token; +} + +TokenVal* val_from_int(int val) { + TokenVal* tval=malloc(sizeof(TokenVal)); + tval->type=intval; + tval->intval=val; + tval->constflag=true; + return tval; +} + +TokenVal* val_from_const_str(const char* val) { + TokenVal* tval=malloc(sizeof(TokenVal)); + tval->type=strval; + tval->strval=val; + tval->constflag=true; + return tval; +} + +TokenVal* val_from_str(char* val) { + TokenVal* tval=malloc(sizeof(TokenVal)); + tval->type=strval; + tval->strval=val; + tval->constflag=false; + return tval; +} + +void print_tok(Token* token) { + Token tok=*token; + if (tok.type>127) { + printf("Token of type "); + switch (tok.type) { + case TYPE_EOF: + printf("EOF"); + break; + case TYPE_IDENT: + printf("IDENT"); + break; + case TYPE_NUM: + printf("NUM"); + break; + default: + break; + } + if (tok.val) { + printf(", and value "); + TokenVal val=*(tok.val); + switch (val.type) { + case intval: + printf("%d\n",val.intval); + break; + case strval: + printf("%s\n",val.strval); + break; + default: + break; + } + } else { + printf("\n"); + } + } else { + printf("Token %c\n",tok.type); + } + +} diff --git a/cinc/tokenize.c b/cinc/tokenize.c new file mode 100644 index 0000000..3b69ebe --- /dev/null +++ b/cinc/tokenize.c @@ -0,0 +1,110 @@ +// +// tokenize.c +// cinc +// +// Created by Peter Terpstra on 9/7/18. +// Copyright © 2018 Peter Terpstra. All rights reserved. +// + +#include "tokenize.h" +#include "token.h" +#include +#include +#include +#include + +#define ID_MAX_SIZE 31 + +Token* next_token(int* strpos, char* prg, Token* prev) { + char current=prg[*strpos]; + if (isalpha(current) || current=='_') { + char* id=malloc(sizeof(char)*ID_MAX_SIZE+1); + int length=1; + id[0]=current; + (*strpos)++; + while (true) { + current=prg[*strpos]; + if (isalnum(current) || current=='_') { + id[length]=current; + length++; + (*strpos)++; + } else { + break; + } + if (length==ID_MAX_SIZE) { + break; + } + } + id[length]=0; + return new_token(TYPE_IDENT, val_from_str(id), prev); + } else if (isdigit(current)) { + char* id=malloc(sizeof(char)*ID_MAX_SIZE+1); + int length=1; + id[0]=current; + (*strpos)++; + while (true) { + current=prg[*strpos]; + if (isdigit(current)) { + id[length]=current; + length++; + (*strpos)++; + } else { + break; + } + if (length==ID_MAX_SIZE) { + break; + } + } + id[length]=0; + return new_token(TYPE_NUM, val_from_int(atoi(id)), prev); + } else if (isblank(current) || current=='\n') { + (*strpos)++; + current=prg[*strpos]; + while (isblank(current)) { + (*strpos)++; + current=prg[*strpos]; + } + return next_token(strpos, prg, prev); + } else if (current=='{' || current=='}' || current=='(' || current==')' || current==';' ) { + (*strpos)++; + return new_token(current, NULL, prev); + } + return new_token(TYPE_EOF, NULL, prev); +} + +Token* tokenize(char* prg) { + int strpos=0; + Token* first=NULL; + Token* tok=NULL; + while (true) { + tok=next_token(&strpos, prg, tok); + if (!first) { + first=tok; + } + if (tok->type==TYPE_EOF) { + break; + } + } + return first; +} + +void free_toklist(Token* tokens) { + while (tokens) { + unsigned char type=tokens->type; + if (tokens->val) { + TokenVal val=*(tokens->val); + if (!(val.constflag)) { + if (val.type==strval) { + free(val.strval); + } + } + } + free(tokens->val); + Token* next=tokens->next; + free(tokens); + tokens=next; + if (type==TYPE_EOF) { + break; + } + } +} diff --git a/cinc/tokenize.h b/cinc/tokenize.h new file mode 100644 index 0000000..09bdba0 --- /dev/null +++ b/cinc/tokenize.h @@ -0,0 +1,17 @@ +// +// tokenize.h +// cinc +// +// Created by Peter Terpstra on 9/7/18. +// Copyright © 2018 Peter Terpstra. All rights reserved. +// + +#ifndef tokenize_h +#define tokenize_h + +#include "token.h" + +Token* tokenize(char* prg); + +void free_toklist(Token* tokens); +#endif /* tokenize_h */