File: ScriptConverter.m

package info (click to toggle)
oolite 1.77.1-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 41,264 kB
  • ctags: 5,362
  • sloc: objc: 132,090; ansic: 10,457; python: 2,225; sh: 1,325; makefile: 332; perl: 259; xml: 125; php: 5
file content (142 lines) | stat: -rw-r--r-- 3,770 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#import <Foundation/Foundation.h>
#import <stdlib.h>
#import "OOLegacyScriptToJavaScriptConverter.h"
#import "OOLogging.h"
#import "OOJSExprNodeHelpers.h"
#import "OOJSExprNode+Graphviz.h"


@interface SimpleProblemReporter: NSObject <OOProblemReportManager>
{
	OOProblemSeverity			_highest;
}
@end


static void TestExprNode();


int main (int argc, const char * argv[])
{
	NSString				*path = nil;
	NSDictionary			*scripts = nil;
	SimpleProblemReporter	*problemReporter = nil;
	NSDictionary			*result = nil;
	NSEnumerator			*scriptEnum = nil;
	NSString				*name = nil;
	
	[[NSAutoreleasePool alloc] init];
	
	if (0)  TestExprNode();
	
	if (argc < 2)
	{
		printf("No file specified.\n");
		return EXIT_FAILURE;
	}
	
	path = [[NSString stringWithUTF8String:argv[1]] stringByStandardizingPath];
	scripts = [NSDictionary dictionaryWithContentsOfFile:path];
	if (scripts == nil)
	{
		printf("Could not open file %s.\n", [path UTF8String]);
		return EXIT_FAILURE;
	}
	
	problemReporter = [[[SimpleProblemReporter alloc] init] autorelease];
	
	result = [OOLegacyScriptToJavaScriptConverter convertMultipleScripts:scripts
																metadata:nil
														 problemReporter:problemReporter];
	
	if (result != nil)
	{
		for (scriptEnum = [result keyEnumerator]; (name = [scriptEnum nextObject]); )
		{
			printf("%s.js:\n\n%s\n-----\n", [name UTF8String], [[result objectForKey:name] UTF8String]);
		}
		return EXIT_SUCCESS;
	}
	else
	{
		printf("Conversion failed.\n");
		return EXIT_FAILURE;
	}
}


@implementation SimpleProblemReporter

- (void) addIssueWithSeverity:(OOProblemSeverity)severity key:(NSString *)key description:(NSString *)description
{
	if (_highest < severity)  _highest = severity;
	
	static const char * const prefixes[] = { "Note", "Note", "Warning", "Unknown selector", "Error", "Bug" };
	if (severity > kOOProblemSeverityBug)  severity = kOOProblemSeverityBug;
	const char *prefix = prefixes[severity];
	
	printf("%s: %s\n", prefix, [description UTF8String]);
}

- (OOProblemSeverity) highestSeverity
{
	return _highest;
}

@end


void OOLogGenericSubclassResponsibilityForFunction(const char *inFunction)
{
	fprintf(stderr, "%s is a subclass responsibility.\n", inFunction);
	abort();
}


#undef NSLog
static void TestExprNode()
{
	OOJSExprNode *node, *x;
	
	x = EX_ID(@"x");
	
	// ~(x--) == ~x--
	node = EX_BITNOT(EX_POST_DECR(x));
	NSLog(@"%@", [node jsCodeRepresentation]);
	
	// (~x)-- (nonsense, just for comparison)
	node = EX_POST_DECR(EX_BITNOT(x));
	NSLog(@"%@", [node jsCodeRepresentation]);
	
	// (~x)-- * y
	node = EX_MULTIPLY(node, EX_ID(@"y"));
	NSLog(@"%@", [node jsCodeRepresentation]);
	
	//x.y["a property"]
	node = EX_PROP(EX_PROP(x, @"y"), @"a property");
	NSLog(@"%@", [node jsCodeRepresentation]);
	
	// value == undefined || value == null
	x = EX_ID(@"value");
	node = EX_OR(EX_EQUAL(x, EX_UNDEFINED), EX_EQUAL(x, EX_NULL));
	NSLog(@"%@", [node jsCodeRepresentation]);
	
	// ((5 + 3) * 5 + 3) ? true : "\tbananas!"
	node = EX_ADD(EX_MULTIPLY(EX_ADD(EX_INT(5), EX_INT(3)), EX_INT(5)), EX_INT(3));
	node = EX_CONDTIONAL(node, EX_TRUE, EX_STR(@"\tbananas!"));
	NSLog(@"%@", [node jsCodeRepresentation]);
	
	node = EX_CALL(EX_PROP(@"foo", @"bar"), node, EX_INT(42), EX_ADD(EX_INT(1), EX_INT(1)));
	NSLog(@"%@", [node jsCodeRepresentation]);
	
	node = EX_CALL(EX_PROP(@"Math", @"floor"), EX_MULTIPLY(EX_VOID_CALL(EX_PROP(@"Math", @"random")), EX_INT(256)));
	NSLog(@"%@", [node jsCodeRepresentation]);
	
	// (1 - 2) + 3 == 1 - 2 + 3
	node = EX_ADD(EX_SUBTRACT(EX_INT(1), EX_INT(2)), EX_INT(3));
	NSLog(@"%@", [node jsCodeRepresentation]);
	
	// 1 - (2 + 3) (the interesting bit is that - and + have same precedence)
	node = EX_SUBTRACT(EX_INT(1), EX_ADD(EX_INT(2), EX_INT(3)));
	NSLog(@"%@", [node jsCodeRepresentation]);
}