001: /*
002: * Copyright 2007 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package com.google.gwt.dev.jjs.impl;
017:
018: import com.google.gwt.dev.jjs.ast.Context;
019: import com.google.gwt.dev.jjs.ast.JBooleanLiteral;
020: import com.google.gwt.dev.jjs.ast.JCharLiteral;
021: import com.google.gwt.dev.jjs.ast.JDoubleLiteral;
022: import com.google.gwt.dev.jjs.ast.JFloatLiteral;
023: import com.google.gwt.dev.jjs.ast.JIntLiteral;
024: import com.google.gwt.dev.jjs.ast.JLongLiteral;
025: import com.google.gwt.dev.jjs.ast.JNullLiteral;
026: import com.google.gwt.dev.jjs.ast.JStringLiteral;
027: import com.google.gwt.dev.jjs.ast.JVisitor;
028: import com.google.gwt.dev.js.ast.JsProgram;
029: import com.google.gwt.dev.js.ast.JsVisitable;
030:
031: import java.math.BigInteger;
032: import java.util.ArrayList;
033: import java.util.Collections;
034: import java.util.List;
035: import java.util.Stack;
036:
037: /**
038: * Translates Java literals into JavaScript literals.
039: */
040: public class GenerateJavaScriptLiterals extends JVisitor {
041:
042: private final JsProgram program;
043: private final Stack<JsVisitable<?>> nodeStack = new Stack<JsVisitable<?>>();
044:
045: public GenerateJavaScriptLiterals(JsProgram program) {
046: this .program = program;
047: }
048:
049: @Override
050: public final void endVisit(JBooleanLiteral x, Context ctx) {
051: push(x.getValue() ? program.getTrueLiteral() : program
052: .getFalseLiteral());
053: }
054:
055: @Override
056: public final void endVisit(JCharLiteral x, Context ctx) {
057: push(program.getIntegralLiteral(BigInteger
058: .valueOf(x.getValue())));
059: }
060:
061: @Override
062: public final void endVisit(JDoubleLiteral x, Context ctx) {
063: push(program.getDecimalLiteral(String.valueOf(x.getValue())));
064: }
065:
066: @Override
067: public final void endVisit(JFloatLiteral x, Context ctx) {
068: push(program.getDecimalLiteral(String.valueOf(x.getValue())));
069: }
070:
071: @Override
072: public final void endVisit(JIntLiteral x, Context ctx) {
073: push(program.getIntegralLiteral(BigInteger
074: .valueOf(x.getValue())));
075: }
076:
077: @Override
078: public final void endVisit(JLongLiteral x, Context ctx) {
079: push(program.getIntegralLiteral(BigInteger
080: .valueOf(x.getValue())));
081: }
082:
083: @Override
084: public final void endVisit(JNullLiteral x, Context ctx) {
085: push(program.getNullLiteral());
086: }
087:
088: @Override
089: public final void endVisit(JStringLiteral x, Context ctx) {
090: push(program.getStringLiteral(x.getValue()));
091: }
092:
093: public final <T extends JsVisitable> T peek() {
094: return (T) nodeStack.peek();
095: }
096:
097: protected final <T extends JsVisitable> T pop() {
098: return (T) nodeStack.pop();
099: }
100:
101: protected final <T extends JsVisitable> List<T> popList(int count) {
102: List<T> list = new ArrayList<T>();
103: while (count > 0) {
104: T item = this .<T> pop();
105: if (item != null) {
106: list.add(item);
107: }
108: --count;
109: }
110: Collections.reverse(list);
111: return list;
112: }
113:
114: protected final <T extends JsVisitable> void popList(
115: List<T> collection, int count) {
116: List<T> list = new ArrayList<T>();
117: while (count > 0) {
118: T item = this .<T> pop();
119: if (item != null) {
120: list.add(item);
121: }
122: --count;
123: }
124: Collections.reverse(list);
125: collection.addAll(list);
126: }
127:
128: protected final <T extends JsVisitable> void push(T node) {
129: nodeStack.push(node);
130: }
131: }
|