// // MyOpenGLCommand.m // cfep // // Created by Nobuki Takayama on 06/02/18. // Copyright 2006 OpenXM.org. All rights reserved. // #import "MyOpenGLCommand.h" #define min(a,b) (a>b?b:a) @implementation MyOpenGLCommand -(id)init { int i; command = nil; opCode = -1; endGroup = NO; for (i=0; i<4; i++) f4[i] = 0.0; for (i=0; i<4; i++) i4[i] = 0; return self; } -(void) dealloc { if (argv) [argv autorelease]; } -(void)print { int i; NSLog(@"command=%s, opCode=%d, [",command,opCode); for (i=0; i<4; i++) NSLog(@"%f,",f4[i]); NSLog(@"; "); for (i=0; i<4; i++) NSLog(@"%d,",i4[i]); NSLog(@"]\n"); } +(MyOpenGLCommand *)allocAndCompile: (NSString *)cmd by: (id) sender { MyOpenGLCommand *c; c=[[MyOpenGLCommand alloc] init]; [c autorelease]; return [c compile: cmd by: sender]; } -(BOOL) isEndGroup { return endGroup; } -(MyOpenGLCommand *) compile: (NSString *)cmd by: (id) sender { // parse cmd and set opCode and f4[]. opCode starts with CFEPgl. // Interpreter code is in MyOpenGLView.m with the name execute: NSArray *a; NSString *s; NSString *errmsg; id x; int fargc, iargc; int i,n,ii; double f; a = [MyUtil arrayOfStringFrom: cmd]; //"[glColor4f,1.0,1.0,0.0,1.0]" -> array of [NSString, MyFloat, MyFloat,..] [a retain]; argv = a; n = [a count]; fargc = 0; iargc = 0; // for (i=0; i\n",s); errmsg = [NSString stringWithFormat: @"Undefined command=<%@>",s]; [sender output: errmsg]; return nil; } // Format glxxxpfqi fargc=p, iargc=q. Example: glxxx4f1i, 0.1,0.2,0.3,0.4,34 if (fargc > 0) { for (i=1; i< min(n,fargc+1); i++) { x = [a objectAtIndex: i]; if ([x isMemberOfClass: [MyFloat class]] == YES) f = [x getFValue]; else { NSLog(@"float is exected.\n"); f = 0.0; } if (i-1 < 4) f4[i-1] = f; } } if (iargc > 0) { for (i=1+fargc; i< min(n,fargc+iargc+1); i++) { x = [a objectAtIndex: i]; if ([x isMemberOfClass: [MyInt class]] == YES) ii = [x getIValue]; else { ii = 0; NSLog(@"int is exected.\n"); } if (i-1-fargc < 4) i4[i-1-fargc] = ii; } } return self; } -(int) getOpCode { return opCode; } -(double *)getF4 { return f4; } -(int *) getI4 { return i4; } -(NSArray *)getArgv { return argv; } @end /* Code to test MyFloat put in compile: MyFloat *x; NSLog(@"member test.\n" x = [MyFloat allocWith: 1.0]; if ([x isMemberOfClass: [MyFloat class]] == YES) NSLog(@"[x class] is MyFloat\n"); if ([x isMemberOfClass: [MyInt class]] == YES) NSLog(@"[x class] is MyInt\n"); */ @implementation MyFloat +(MyFloat *)allocWith: (float) f { MyFloat *ff; ff=[MyFloat alloc]; [ff setFValue: f]; return ff; } -(void) setFValue: (float) f { fValue = f; } -(float) getFValue { return fValue; } -(NSString *)description { return [NSString stringWithFormat: @"(float)%f",fValue]; } @end @implementation MyInt +(MyInt *)allocWith: (int) i { MyInt *ii; ii=[MyInt alloc]; [ii setIValue: i]; return ii; } -(void) setIValue: (int) i { iValue = i; } -(int) getIValue { return iValue; } -(NSString *)description { return [NSString stringWithFormat: @"(int)%d",iValue]; } @end #define SMAX 0x1000 //4096*16 @implementation MyUtil static int debugMyUtil = 0; +(void) setDebugMyUtil { if (debugMyUtil) debugMyUtil = 0; else debugMyUtil = 1; } +(NSMutableArray *) arrayOfStringFrom: (NSString *) args { int n,i,k,type; unichar c; unichar s[SMAX]; NSMutableArray *a; // for debug NSMutableArray *aa; NSString *ss; int status,level; status = -1; level = 0; type = 0; n = [args length]; if (n >= SMAX-1) { NSLog(@"Too big string for stringToArrayOfString.\n"); return nil; } a = [NSMutableArray arrayWithCapacity: 1]; aa = [NSMutableArray arrayWithCapacity: 1]; for (i=0, k=0; i ' ') { k=0; status = 2; type=0; s[k] = c; k++; // type 0 : float } else if (c == ']') status = -1; break; case 2: if ((c == ',') || (c == ']')) { if (c == ',') status = 0; else status = -1; if (k > 0) ss = [NSString stringWithCharacters: s length: k]; else ss = @""; [a addObject: ss]; if (type == 2) [aa addObject: [MyInt allocWith: [ss intValue]]]; else if (type == 0) [aa addObject: [MyFloat allocWith: [ss floatValue]]]; else [aa addObject: ss]; }else{ s[k] = c; k++; } break; case 1: if (c == '[') { s[k] = c; k++; level++; }else if (c == ']') { s[k] = ']'; k++; if (k > 0) ss = [NSString stringWithCharacters: s length: k]; else ss = @""; [a addObject: ss]; [aa addObject: [MyUtil arrayOfStringFrom: ss]]; status = 0; }else { s[k] = c; k++; } break; case 21: if (c == ')') { if ((s[0] == 'i') && (s[1] == 'n') && (s[2] == 't')) type=2; else type = 0; status = 2; k = 0; // starting to read data. }else if (c > ' ') { s[k] = c; k++; } break; } }else if (level > 0) { s[k] = c; k++; if (c == '[') level++; else if (c == ']') level--; } } if (debugMyUtil) { NSLog(@"a=%@\n",a); NSLog(@"aa=%@\n",aa); } return aa; } /* NSLog(@"mytest=%@\n",[MyUtil arrayOfStringFrom: @"[]"]); NSLog(@"mytest=%@\n",[MyUtil arrayOfStringFrom: @"[1,[]]"]); NSLog(@"mytest=%@\n",[MyUtil arrayOfStringFrom: @"[1,2,[3,4],5]"]); NSLog(@"mytest=%@\n",[MyUtil arrayOfStringFrom: @" [1, 2,3, 4,5]"]); NSLog(@"mytest=%@\n",[MyUtil arrayOfStringFrom: @"[[1],2,[[3,[4]]],5]"]); // There is a bug. Output is 1,2,... */ @end