Permalink
Browse files

关于对象的修修补补

  • Loading branch information...
1 parent e32825a commit 8b58112ab53324f02b3a0fdef44b5dcfae4f82fd @jiyinyiyong jiyinyiyong committed Apr 3, 2012
Showing with 41 additions and 28 deletions.
  1. +9 −2 code.lx
  2. +12 −16 parse.coffee
  3. +20 −10 target.js
View
11 code.lx
@@ -27,6 +27,13 @@ write code with 2 spaces in front!
then, detailed varable names needed, the ideas below will be mixed:
1) 'object/attribute' converts to 'object.attribute'
2) pounctions like '!' in name change with '_', 'a!' to 'a_1'
-4) '(/method object args)' converts to 'object.method(args)'
- /call obj 3 (arr 2)
+ console/log '\nmore testing'
+ console/log 3 (arr 2)
+ var a \ obj
+ a 2
+ make-obj 3
+ func \ fn (a b)
+ return (+ b 1)
+ console/log \ a/func 3
+ console/log a/make-obj
View
28 parse.coffee
@@ -153,15 +153,16 @@ expend = (arr) ->
else
if head in ['+', '-', '*', '/', '%', '<', '>']
exp = calculate head, body
- if (image = head.match /^((\w+\/)*\w+)$/)?
+ else if (image = head.match f_available)?
exp = run_function head, body
- if (image = head.match /^\/.+/)
- exp = run_method head, body
exp
exp_judge = (x) ->
if typeof x is 'string'
- return x
+ if x.match f_available
+ return va x
+ else
+ return x
if typeof x is 'object'
return expend x
throw new Error 'wrong type for calculate'
@@ -170,21 +171,22 @@ calculate = (head, body) ->
exp = (body.map exp_judge).join head
"#{exp}"
+f_available = /^[\w\!\?@#\$\%\^\&\*\-\=\+:\/]+/
run_function = (head, body) ->
body = body.map exp_judge
- head = head.replace /\//g, '.'
+ head = va head
"#{head}(#{body})"
declare_varable = (arr) ->
- varable = arr[0]
+ varable = va arr[0]
value = arr[1]
if typeof varable is 'string'
value = exp_judge value
return "var #{varable} = #{value}"
else throw new Error 'wrong type in declare'
assign_varable = (arr) ->
- varable = arr[0]
+ varable = va arr[0]
value = arr[1]
if typeof varable is 'string'
value = exp_judge value
@@ -200,7 +202,7 @@ make_object = (arr) ->
exp = []
for item in arr
if typeof item[0] is 'string'
- index = item[0]
+ index = va item[0]
value = exp_judge item[1]
exp.push "#{index}: #{value}"
else throw new Error 'err in making obj'
@@ -211,7 +213,7 @@ define_function = (arr) ->
args = ''
if typeof func is 'object'
args = func[1..].join ', '
- func = func[0]
+ func = va func[0]
body = sequence arr[1..]
"function #{func}(#{args}){#{body}}"
@@ -248,7 +250,7 @@ for_loop = (arr) ->
exp += "#{key} = _i;"
else
exp += 'delete _i;'
- exp + "#{body}}"
+ exp += "#{body}}"
va_list =
'!': '1'
@@ -287,12 +289,6 @@ va = (str) ->
throw new Error "varable cant be recognized"
exp
-run_method = (head, body) ->
- unless body[0]? then throw new Error 'err in method'
- obj = body[0]
- args = (body[1..].map exp_judge).join ', '
- "#{obj}.#{head[1..]}(#{args})"
-
target = sequence source_array
beautify = (require './beautify').js_beautify
View
30 target.js
@@ -1,32 +1,42 @@
1 + 2;
-console.log("hello world");
+console['log']("hello world");
var a = 3;
-console.log(a = 1 + 2 + 2 + 3);
+console['log'](a = 1 + 2 + 2 + 3);
var b = [1, 2, 4, [1, 2, 4]];
-console.log({
+console['log']({
a: 3,
b: '33'
});
var ss = true;
function aa(a, b) {
- console.log(true);
+ console['log'](true);
};
if (4 > 3) {
- console.log("4>3");
+ console['log']("4>3");
} else if (4 > 5) {
- console.log("4>5");
+ console['log']("4>5");
} else {
- console.log('else');
+ console['log']('else');
};
a = 5;
while (a > 3) {
- console.log(3);
+ console['log'](3);
a = a - 1;
};
for (var _i in [1, 2, 444]) {
a = [1, 2, 444][_i];
delete _i;
- console.log(a);
+ console['log'](a);
};
-obj.call(3, [2]);
+console['log']('\nmore testing');
+console['log'](3, [2]);
+var a = {
+ a: 2,
+ make_0obj: 3,
+ func: function a(b) {
+ return (b + 1);
+ }
+};
+console['log'](a['func'](3));
+console['log'](a['make_0obj']);

0 comments on commit 8b58112

Please sign in to comment.