From 1df642942d9366d5f523567ddffae26018f57856 Mon Sep 17 00:00:00 2001 From: Steve Bennett Date: Sun, 28 Feb 2016 12:03:53 +1000 Subject: Small code cleanups Signed-off-by: Steve Bennett --- jim.c | 56 +++++++++++++++++++++++--------------------------------- 1 file changed, 23 insertions(+), 33 deletions(-) (limited to 'jim.c') diff --git a/jim.c b/jim.c index 04a7e7e..b1181a3 100644 --- a/jim.c +++ b/jim.c @@ -1151,11 +1151,6 @@ void Jim_FreeStackElements(Jim_Stack *stack, void (*freeFunc) (void *ptr)) #define TOKEN_IS_SEP(type) (type >= JIM_TT_SEP && type <= JIM_TT_EOF) -/* Parser states */ -#define JIM_PS_DEF 0 /* Default state */ -#define JIM_PS_QUOTE 1 /* Inside "" */ -#define JIM_PS_DICTSUGAR 2 /* Tokenising abc(def) into 4 separate tokens */ - /** * Results of missing quotes, braces, etc. from parsing. */ @@ -1176,7 +1171,7 @@ struct JimParserCtx int tline; /* Line number of the returned token */ int tt; /* Token type */ int eof; /* Non zero if EOF condition is true. */ - int state; /* Parser state */ + int inquote; /* Parsing a quoted string */ int comment; /* Non zero if the next chars may be a comment. */ struct JimParseMissing missing; /* Details of any missing quotes, etc. */ }; @@ -1206,7 +1201,7 @@ static void JimParserInit(struct JimParserCtx *pc, const char *prg, int len, int pc->tline = 0; pc->tt = JIM_TT_NONE; pc->eof = 0; - pc->state = JIM_PS_DEF; + pc->inquote = 0; pc->linenr = linenr; pc->comment = 1; pc->missing.ch = ' '; @@ -1226,7 +1221,7 @@ static int JimParseScript(struct JimParserCtx *pc) } switch (*(pc->p)) { case '\\': - if (*(pc->p + 1) == '\n' && pc->state == JIM_PS_DEF) { + if (*(pc->p + 1) == '\n' && !pc->inquote) { return JimParseSep(pc); } pc->comment = 0; @@ -1235,14 +1230,14 @@ static int JimParseScript(struct JimParserCtx *pc) case '\t': case '\r': case '\f': - if (pc->state == JIM_PS_DEF) + if (!pc->inquote) return JimParseSep(pc); pc->comment = 0; return JimParseStr(pc); case '\n': case ';': pc->comment = 1; - if (pc->state == JIM_PS_DEF) + if (!pc->inquote) return JimParseEol(pc); return JimParseStr(pc); case '[': @@ -1643,7 +1638,7 @@ static int JimParseStr(struct JimParserCtx *pc) return JimParseBrace(pc); } if (*pc->p == '"') { - pc->state = JIM_PS_QUOTE; + pc->inquote = 1; pc->p++; pc->len--; /* In case the end quote is missing */ @@ -1654,7 +1649,7 @@ static int JimParseStr(struct JimParserCtx *pc) pc->tline = pc->linenr; while (1) { if (pc->len == 0) { - if (pc->state == JIM_PS_QUOTE) { + if (pc->inquote) { pc->missing.ch = '"'; } pc->tend = pc->p - 1; @@ -1663,7 +1658,7 @@ static int JimParseStr(struct JimParserCtx *pc) } switch (*pc->p) { case '\\': - if (pc->state == JIM_PS_DEF && *(pc->p + 1) == '\n') { + if (!pc->inquote && *(pc->p + 1) == '\n') { pc->tend = pc->p - 1; pc->tt = JIM_TT_ESC; return JIM_OK; @@ -1711,7 +1706,7 @@ static int JimParseStr(struct JimParserCtx *pc) case '\r': case '\f': case ';': - if (pc->state == JIM_PS_DEF) { + if (!pc->inquote) { pc->tend = pc->p - 1; pc->tt = JIM_TT_ESC; return JIM_OK; @@ -1721,12 +1716,12 @@ static int JimParseStr(struct JimParserCtx *pc) } break; case '"': - if (pc->state == JIM_PS_QUOTE) { + if (pc->inquote) { pc->tend = pc->p - 1; pc->tt = JIM_TT_ESC; pc->p++; pc->len--; - pc->state = JIM_PS_DEF; + pc->inquote = 0; return JIM_OK; } break; @@ -1785,8 +1780,7 @@ static int odigitval(int c) /* Perform Tcl escape substitution of 's', storing the result * string into 'dest'. The escaped string is guaranteed to * be the same length or shorted than the source string. - * Slen is the length of the string at 's', if it's -1 the string - * length will be calculated by the function. + * Slen is the length of the string at 's'. * * The function returns the length of the resulting string. */ static int JimEscape(char *dest, const char *s, int slen) @@ -1794,9 +1788,6 @@ static int JimEscape(char *dest, const char *s, int slen) char *p = dest; int i, len; - if (slen == -1) - slen = strlen(s); - for (i = 0; i < slen; i++) { switch (s[i]) { case '\\': @@ -12433,7 +12424,8 @@ static int Jim_LsearchCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const * static int Jim_LappendCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) { Jim_Obj *listObjPtr; - int shared, i; + int new_obj = 0; + int i; if (argc < 2) { Jim_WrongNumArgs(interp, 1, argv, "varName ?value value ...?"); @@ -12443,18 +12435,16 @@ static int Jim_LappendCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const * if (!listObjPtr) { /* Create the list if it does not exist */ listObjPtr = Jim_NewListObj(interp, NULL, 0); - if (Jim_SetVariable(interp, argv[1], listObjPtr) != JIM_OK) { - Jim_FreeNewObj(interp, listObjPtr); - return JIM_ERR; - } + new_obj = 1; } - shared = Jim_IsShared(listObjPtr); - if (shared) + else if (Jim_IsShared(listObjPtr)) { listObjPtr = Jim_DuplicateObj(interp, listObjPtr); + new_obj = 1; + } for (i = 2; i < argc; i++) Jim_ListAppendElement(interp, listObjPtr, argv[i]); if (Jim_SetVariable(interp, argv[1], listObjPtr) != JIM_OK) { - if (shared) + if (new_obj) Jim_FreeNewObj(interp, listObjPtr); return JIM_ERR; } @@ -12666,22 +12656,22 @@ static int Jim_AppendCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *a return JIM_ERR; } else { - int freeobj = 0; + int new_obj = 0; stringObjPtr = Jim_GetVariable(interp, argv[1], JIM_UNSHARED); if (!stringObjPtr) { /* Create the string if it doesn't exist */ stringObjPtr = Jim_NewEmptyStringObj(interp); - freeobj = 1; + new_obj = 1; } else if (Jim_IsShared(stringObjPtr)) { - freeobj = 1; + new_obj = 1; stringObjPtr = Jim_DuplicateObj(interp, stringObjPtr); } for (i = 2; i < argc; i++) { Jim_AppendObj(interp, stringObjPtr, argv[i]); } if (Jim_SetVariable(interp, argv[1], stringObjPtr) != JIM_OK) { - if (freeobj) { + if (new_obj) { Jim_FreeNewObj(interp, stringObjPtr); } return JIM_ERR; -- cgit v1.1