]> git.sur5r.net Git - cc65/commitdiff
Added string handling functions: .STRLEN and .STRAT
authorcuz <cuz@b7a2c559-68d2-44c3-8de9-860c34a00d81>
Tue, 25 Jul 2000 21:32:11 +0000 (21:32 +0000)
committercuz <cuz@b7a2c559-68d2-44c3-8de9-860c34a00d81>
Tue, 25 Jul 2000 21:32:11 +0000 (21:32 +0000)
git-svn-id: svn://svn.cc65.org/cc65/trunk@199 b7a2c559-68d2-44c3-8de9-860c34a00d81

src/ca65/expr.c
src/ca65/pseudo.c
src/ca65/scanner.c
src/ca65/scanner.h

index 0bad48328d373f969f4c273223122303c04df1c7..fc43cdcb8e0f89306fca6a80d88920f88576ad58 100644 (file)
@@ -467,6 +467,71 @@ static int FuncReferenced (void)
 
 
 
+static int FuncStrAt (void)
+/* Handle the .STRAT function */
+{
+    char Str [sizeof(SVal)];
+    long Index;
+
+    /* String constant expected */
+    if (Tok != TOK_STRCON) {
+       Error (ERR_STRCON_EXPECTED);
+       NextTok ();
+               return 0;
+
+    }
+
+    /* Remember the string and skip it */
+    strcpy (Str, SVal);
+    NextTok ();
+
+    /* Comma must follow */
+    ConsumeComma ();
+
+    /* Expression expected */
+    Index = ConstExpression ();
+
+    /* Must be a valid index */
+    if (Index >= strlen (Str)) {
+       Error (ERR_RANGE);
+       return 0;
+    }
+
+    /* Return the char, handle as unsigned */
+    return (unsigned char) Str[(size_t)Index];
+}
+
+
+
+static int FuncStrLen (void)
+/* Handle the .STRLEN function */
+{
+    /* String constant expected */
+    if (Tok != TOK_STRCON) {
+
+       Error (ERR_STRCON_EXPECTED);
+       /* Smart error recovery */
+       if (Tok != TOK_RPAREN) {
+           NextTok ();
+       }
+               return 0;
+
+    } else {
+
+        /* Get the length of the string */
+       int Len = strlen (SVal);
+
+       /* Skip the string */
+       NextTok ();
+
+       /* Return the length */
+       return Len;
+
+    }
+}
+
+
+
 static int FuncTCount (void)
 /* Handle the .TCOUNT function */
 {
@@ -492,7 +557,7 @@ static int FuncTCount (void)
        switch (Tok) {
            case TOK_LPAREN:    ++Parens;       break;
            case TOK_RPAREN:    --Parens;       break;
-           default:                            break;
+           default:                            break;
        }
 
        /* Skip the token */
@@ -547,10 +612,10 @@ static ExprNode* Factor (void)
     SymEntry* S;
 
     switch (Tok) {
-                 
+
        case TOK_INTCON:
        case TOK_CHARCON:
-           N = LiteralExpr (IVal);
+           N = LiteralExpr (IVal);
                    NextTok ();
            break;
 
@@ -568,7 +633,7 @@ static ExprNode* Factor (void)
                    /* Create symbol node */
                    N = NewExprNode ();
                    N->Op    = EXPR_SYMBOL;
-                   N->V.Sym = S;
+                   N->V.Sym = S;
                }
                NextTok ();
            }
@@ -658,6 +723,14 @@ static ExprNode* Factor (void)
            N = Function (FuncReferenced);
            break;
 
+       case TOK_STRAT:
+           N = Function (FuncStrAt);
+           break;
+
+       case TOK_STRLEN:
+           N = Function (FuncStrLen);
+           break;
+
        case TOK_TCOUNT:
            N = Function (FuncTCount);
            break;
index 7dd9c19e11cbeeba5771cbe1593fa6abb9cf5b78..ada1f4951389f7ecf56dbc77bc149be30918a2de 100644 (file)
@@ -1162,7 +1162,9 @@ static CtrlDesc CtrlCmdTab [] = {
     { ccNone,          DoROData        },
     { ccNone,          DoSegment       },
     { ccNone,          DoSmart         },
+    { ccNone,          DoUnexpected    },      /* .STRAT */
     { ccNone,                  DoUnexpected    },      /* .STRING */
+    { ccNone,          DoUnexpected    },      /* .STRLEN */
     { ccNone,          DoSunPlus       },
     { ccNone,          DoUnexpected    },      /* .TCOUNT */
     { ccNone,          DoWord          },
index e51009cfa7984f1f098c6c004b1e53080e3b0c8f..6d89dc320e0751fde01ec46e516d1dca473a04ac 100644 (file)
@@ -192,7 +192,7 @@ struct DotKeyword {
     { "LOCALCHAR",     TOK_LOCALCHAR   },
     { "MAC",           TOK_MACRO       },
     { "MACPACK",       TOK_MACPACK     },
-    { "MACRO",         TOK_MACRO       },
+    { "MACRO",         TOK_MACRO       },
     { "MATCH",         TOK_MATCH       },
     { "MID",                   TOK_MID         },
     { "MOD",           TOK_MOD         },
@@ -213,19 +213,21 @@ struct DotKeyword {
     { "RELOC",         TOK_RELOC       },
     { "REPEAT",                TOK_REPEAT      },
     { "RES",           TOK_RES         },
-    { "RIGHT",         TOK_RIGHT       },
-    { "RODATA",                TOK_RODATA      },
-    { "SEGMENT",       TOK_SEGMENT     },
-    { "SHL",           TOK_SHL         },
-    { "SHR",           TOK_SHR         },
-    { "SMART",         TOK_SMART       },
-    { "STRING",                TOK_STRING      },
-    { "SUNPLUS",       TOK_SUNPLUS     },
-    { "TCOUNT",                TOK_TCOUNT      },
-    { "WORD",          TOK_WORD        },
-    { "XMATCH",                TOK_XMATCH      },
-    { "XOR",           TOK_BXOR        },
-    { "ZEROPAGE",      TOK_ZEROPAGE    },
+    { "RIGHT",         TOK_RIGHT       },
+    { "RODATA",                TOK_RODATA      },
+    { "SEGMENT",       TOK_SEGMENT     },
+    { "SHL",           TOK_SHL         },
+    { "SHR",           TOK_SHR         },
+    { "SMART",         TOK_SMART       },
+    { "STRAT",         TOK_STRAT       },
+    { "STRING",                TOK_STRING      },
+    { "STRLEN",                TOK_STRLEN      },
+    { "SUNPLUS",       TOK_SUNPLUS     },
+    { "TCOUNT",                TOK_TCOUNT      },
+    { "WORD",          TOK_WORD        },
+    { "XMATCH",                TOK_XMATCH      },
+    { "XOR",           TOK_BXOR        },
+    { "ZEROPAGE",      TOK_ZEROPAGE    },
 };
 
 
index 8f5e4486ca6fb159177fc5ad2bfff90ff5b0e29a..d180f0c6690b49e0d8e127ee3ff1993159281a15 100644 (file)
@@ -188,7 +188,9 @@ enum Token {
     TOK_RODATA,
     TOK_SEGMENT,
     TOK_SMART,
+    TOK_STRAT,
     TOK_STRING,
+    TOK_STRLEN,
     TOK_SUNPLUS,
     TOK_TCOUNT,
     TOK_WORD,