tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
71
Task/S-Expressions/Java/s-expressions-5.java
Normal file
71
Task/S-Expressions/Java/s-expressions-5.java
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
package jfkbits;
|
||||
|
||||
import java.util.AbstractCollection;
|
||||
import java.util.Arrays;
|
||||
import java.util.Iterator;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import jfkbits.LispParser.Expr;
|
||||
|
||||
public class ExprList extends ArrayList<Expr> implements Expr
|
||||
{
|
||||
ExprList parent = null;
|
||||
int indent =1;
|
||||
|
||||
public int getIndent()
|
||||
{
|
||||
if (parent != null)
|
||||
{
|
||||
return parent.getIndent()+indent;
|
||||
}
|
||||
else return 0;
|
||||
}
|
||||
|
||||
public void setIndent(int indent)
|
||||
{
|
||||
this.indent = indent;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void setParent(ExprList parent)
|
||||
{
|
||||
this.parent = parent;
|
||||
}
|
||||
|
||||
public String toString()
|
||||
{
|
||||
String indent = "";
|
||||
if (parent != null && parent.get(0) != this)
|
||||
{
|
||||
indent = "\n";
|
||||
char[] chars = new char[getIndent()];
|
||||
Arrays.fill(chars, ' ');
|
||||
indent += new String(chars);
|
||||
}
|
||||
|
||||
String output = indent+"(";
|
||||
for(Iterator<Expr> it=this.iterator(); it.hasNext(); )
|
||||
{
|
||||
Expr expr = it.next();
|
||||
output += expr.toString();
|
||||
if (it.hasNext())
|
||||
output += " ";
|
||||
}
|
||||
output += ")";
|
||||
return output;
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized boolean add(Expr e)
|
||||
{
|
||||
if (e instanceof ExprList)
|
||||
{
|
||||
((ExprList) e).setParent(this);
|
||||
if (size() != 0 && get(0) instanceof Atom)
|
||||
((ExprList) e).setIndent(2);
|
||||
}
|
||||
return super.add(e);
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue