Skip to content
Snippets Groups Projects
Commit dd686d49 authored by olale's avatar olale
Browse files

Moved assignment code to clean repository

parents
Branches
No related tags found
No related merge requests found
Showing
with 800 additions and 0 deletions
<?xml version="1.0" encoding="UTF-8"?>
<project name="MyProject" default="dist" basedir=".">
<description>
simple example build file
</description>
<!-- set global properties for this build -->
<property name="src" location="src"/>
<property name="build" location="build"/>
<property name="dist" location="dist"/>
<target name="init">
<!-- Create the time stamp -->
<tstamp/>
<!-- Create the build directory structure used by compile -->
<mkdir dir="${build}"/>
</target>
<target name="compile" depends="init"
description="compile the source " >
<!-- Compile the java code from ${src} into ${build} -->
<javac srcdir="${src}" destdir="${build}"/>
</target>
<target name="dist" depends="compile"
description="generate the distribution" >
<!-- Create the distribution directory -->
<mkdir dir="${dist}/lib"/>
<!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file -->
<jar jarfile="${dist}/lib/MyProject-${DSTAMP}.jar" basedir="${build}"/>
</target>
<target name="clean"
description="clean up" >
<!-- Delete the ${build} and ${dist} directory trees -->
<delete dir="${build}"/>
<delete dir="${dist}"/>
</target>
</project>
\ No newline at end of file
!domain.BuildConfig
project:
description: "\n simple example build file\n "
properties:
-
!domain.Property
name: src
location: src
-
!domain.Property
name: build
location: build
-
!domain.Property
name: dist
location: dist
targets:
- &init
!domain.Target
name: init
- &compile
!domain.Target
name: compile
description: "compile the source "
depends:
- *init
- &dist
!domain.Target
name: dist
description: "generate the distribution"
depends:
- *compile
- &clean
!domain.Target
name: clean
description: "clean up"
name: MyProject
basedir: "."
\ No newline at end of file
File added
package domain;
public class BuildConfig {
private Project project;
public Project getProject() {
return project;
}
public void setProject(Project project) {
this.project = project;
}
@Override
public String toString() {
return "[BuildInfo: "+getProject()+"]";
}
}
package domain;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
public class Project {
private String description;
private String name;
private Target defaultTarget;
private String basedir;
private List<Property> properties;
private List<Target> targets;
private String src;
private String build;
private String dist;
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getSrc() {
return src;
}
public void setSrc(String src) {
this.src = src;
}
public String getBuild() {
return build;
}
public void setBuild(String build) {
this.build = build;
}
public String getDist() {
return dist;
}
public void setDist(String dist) {
this.dist = dist;
}
public List<Target> getTargets() {
return targets;
}
public void setTargets(List<Target> targets) {
this.targets = targets;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Target getDefaultTarget() {
return defaultTarget;
}
public void setDefaultTarget(Target defaultTarget) {
this.defaultTarget = defaultTarget;
}
public String getBasedir() {
return basedir;
}
public void setBasedir(String basedir) {
this.basedir = basedir;
}
public List<Property> getProperties() {
return properties;
}
public void setProperties(List<Property> properties) {
this.properties = properties;
}
@Override
public String toString() {
return "[Project: "+getName()+"]";
}
public Optional<Target> getTargetByName(String nodeName) {
return targets.stream().filter(t -> t.getName().equals(nodeName)).findFirst();
}
public void addTarget(Target target) {
if (targets == null) {
targets = new ArrayList<Target>();
}
targets.add(target);
}
}
package domain;
public class Property {
private String name;
private String location;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
}
package domain;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
public class Target {
private String name;
private String description;
private List<Target> depends;
private String tstamp;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public List<Target> getDepends() {
return depends;
}
public void setDepends(List<Target> depends) {
this.depends = depends;
}
public String getTstamp() {
return tstamp;
}
public void setTstamp(String tstamp) {
this.tstamp = tstamp;
}
public void addDependency(Target targetDependency) {
if (depends == null) {
depends = new ArrayList<Target>();
}
depends.add(targetDependency);
}
@Override
public String toString() {
return MessageFormat.format("Target {0}, Deps: {1}", name, depends);
}
}
package main;
import domain.BuildConfig;
import domain.Project;
import xml.Build;
import xml.XMLBuildConfigurationReader;
import yaml.Compile;
import yaml.YamlBuildConfigurationReader;
public class Main {
public static void main(String[] args) {
XMLBuildConfigurationReader buildConfigurationReader = new XMLBuildConfigurationReader("build.xml");
final Project xmlProject = buildConfigurationReader.getProject();
Build build = new Build(xmlProject);
build.build(1, "dist");
YamlBuildConfigurationReader yamlConfigReader = new YamlBuildConfigurationReader("build.yaml");
final BuildConfig yamlBuildConfig = yamlConfigReader.getBuildConfig();
Compile compile = new Compile(yamlBuildConfig, "dist");
compile.build(1);
}
}
package xml;
import java.text.MessageFormat;
import java.util.List;
import java.util.Optional;
import domain.Project;
import domain.Target;
public class Build {
private Project project;
public Build(Project xmlProject) {
project = xmlProject;
}
public void build(int debuglevel, String targetName) {
if (debuglevel > 0) {
System.out.println(MessageFormat.format("Building {0}", project.getName()));
}
Optional<Target> target = project.getTargetByName(targetName);
if (target.isPresent()) {
final Target actualTarget = target.get();
buildRecursively(debuglevel, actualTarget);
} else {
System.err.println(MessageFormat.format("Invalid target {0}", targetName));
}
}
private void buildRecursively(int debuglevel, final Target actualTarget) {
List<Target> deps = actualTarget.getDepends();
if (deps != null) {
deps.forEach(t -> buildRecursively(debuglevel, t));
}
build(debuglevel, actualTarget);
}
private void build(int debuglevel, Target t) {
if (debuglevel > 0) {
System.out.println(MessageFormat.format("Building target {0}",t.getName()));
}
}
}
package xml;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.Optional;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import domain.Project;
import domain.Target;
public class XMLBuildConfigurationReader {
private final class NodeIterator implements Iterator<Node> {
private final NodeList childNodes;
int i = 0;
private NodeIterator(NodeList childNodes) {
this.childNodes = childNodes;
}
@Override
public boolean hasNext() {
return i < childNodes.getLength();
}
@Override
public Node next() {
Node n = childNodes.item(i);
i++;
return n;
}
}
private Element docElement;
public XMLBuildConfigurationReader(String fileName) {
DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder docBuilder = f.newDocumentBuilder();
Document doc = docBuilder.parse(new File(fileName));
docElement = doc.getDocumentElement();
} catch (ParserConfigurationException | SAXException | IOException e) {
e.printStackTrace();
}
}
private String getAttributeValue(Element elt, String attributeName) {
return elt.getAttributes().getNamedItem(attributeName).getNodeValue();
}
public Project getProject() {
Project project = new Project();
project.setName(getAttributeValue(docElement,"name"));
project.setBasedir(getAttributeValue(docElement,"basedir"));
NodeList childNodes = docElement.getChildNodes();
for (int i = 0; i < childNodes.getLength(); i++) {
Node node = childNodes.item(i);
if (node.getNodeName().equals("target")) {
Target target = new Target();
target.setName(node.getAttributes().getNamedItem("name").getNodeValue());
Node dep = node.getAttributes().getNamedItem("depends");
project.addTarget(target);
if (dep != null) {
Optional<Target> optionalDep = project.getTargetByName(dep.getNodeValue());
if (optionalDep.isPresent()) {
Target targetDependency = optionalDep.get();
target.addDependency(targetDependency);
} else {
System.err.println("invalid target: "+dep.getLocalName());
}
}
}
}
return project;
}
}
package yaml;
import java.text.MessageFormat;
import java.util.List;
import java.util.Optional;
import domain.BuildConfig;
import domain.Project;
import domain.Target;
public class Compile {
private Project project;
private String targetName;
public Compile(BuildConfig yamlBuildConfig, String targetName) {
project = yamlBuildConfig.getProject();
this.targetName = targetName;
}
public void build(int debuglevel) {
if (debuglevel > 0) {
System.out.println(project.getDescription());
}
Optional<Target> target = project.getTargetByName(targetName);
int indent = 0;
if (target.isPresent()) {
final Target actualTarget = target.get();
buildRecursively(debuglevel, actualTarget, indent);
} else {
System.err.println(MessageFormat.format("Invalid target {0}",
targetName));
}
}
private void buildRecursively(int debuglevel, final Target actualTarget, int indent) {
List<Target> deps = actualTarget.getDepends();
if (deps != null) {
deps.forEach(t -> buildRecursively(debuglevel, t, indent+1));
}
build(debuglevel, actualTarget, indent);
}
private void build(int debuglevel, Target t, int indent) {
for (int i = 0; i < indent; i++) {
System.out.print(" ");
}
if (debuglevel > 0) {
System.out.println(MessageFormat.format("Building target {0}: ",
t.getName(), t.getDescription()));
}
}
}
package yaml;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import net.sourceforge.yamlbeans.YamlException;
import net.sourceforge.yamlbeans.YamlReader;
import domain.BuildConfig;
import domain.Project;
public class YamlBuildConfigurationReader {
private YamlReader yamlReader;
public YamlBuildConfigurationReader(String fileName) {
try {
yamlReader = new YamlReader(new FileReader(new File(fileName)));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public BuildConfig getBuildConfig() {
BuildConfig config = null;
try {
config = yamlReader.read(BuildConfig.class);
} catch (YamlException e) {
e.printStackTrace();
}
return config;
}
}
package xml;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import domain.Project;
public class XMLBuildConfigurationReaderTest {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
}
private XMLBuildConfigurationReader confReader;
@Before
public void setUp() throws Exception {
confReader = new XMLBuildConfigurationReader("build.xml");
}
@After
public void tearDown() throws Exception {
}
@Test
public final void test() {
final Project project = confReader.getProject();
assertNotNull(project);
assertEquals("MyProject", project.getName());
assertEquals(".", project.getBasedir());
//assertNotNull(project.getTargets());
}
}
package xml;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;
import yaml.YamlBuildConfigurationReader;
import domain.BuildConfig;
public class YamlBuildConfigurationReaderTest {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
}
private YamlBuildConfigurationReader yamlReader;
@Before
public void setUp() throws Exception {
yamlReader = new YamlBuildConfigurationReader("build.yaml");
}
@After
public void tearDown() throws Exception {
}
@Test
public final void test() {
BuildConfig buildConfig = yamlReader.getBuildConfig();
assertEquals("MyProject", buildConfig.getProject().getName());
}
}
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
<classpathentry kind="output" path="bin"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Visitor_Interpreter</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
Task C/bin/visitor/overview.png

62.2 KiB

package interpreter.lisp;
import java.util.ArrayList;
import java.util.List;
/**
*
* All built-in functions are represented using this class. We declare to
* virtual parameters "param1" and "param2" for all binary functions.
*
* @author olale
*
*/
public abstract class BinaryExpression<T extends Expr> implements Expr, Fun {
private static Symbol param1 = new Symbol("param1");
private static Symbol param2 = new Symbol("param2");
private static ArrayList<Symbol> parameters;
static {
parameters = new ArrayList<Symbol>();
parameters.add(param1);
parameters.add(param2);
}
@SuppressWarnings("unchecked")
@Override
public Expr evaluate(Context context) {
T value1 = (T) context.get(param1);
T value2 = (T) context.get(param2);
return evalBinaryExpression(value1, value2);
}
abstract Expr evalBinaryExpression(T value1, T value2);
@Override
public List<Symbol> getParameters() {
return parameters;
}
@Override
public Expr getBody() {
return this;
}
}
\ No newline at end of file
package interpreter.lisp;
import java.util.ArrayList;
import java.util.List;
public class CompoundExpression implements Expr {
private List<Expr> elements = new ArrayList<Expr>();
public void add(Expr node) {
elements.add(node);
}
public List<Expr> getElements() {
return elements;
}
public Expr evaluate(Context context) {
if (elements.isEmpty()) {
return Constants.EMPTY_LIST;
}
if (elements.get(0) instanceof Symbol) {
String keyword = ((Symbol) elements.get(0)).getValue();
if (keyword.equals("def")) {
return new CompoundFunction(elements.get(1), elements.get(2),
elements.get(3)).evaluate(context);
} else if (keyword.equals("if")) {
return new Conditional(elements.get(1), elements.get(2),
elements.get(3)).evaluate(context);
}
}
return new FunctionCall((Symbol) elements.get(0),elements.subList(1,elements.size())).evaluate(context);
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
List<Expr> e = elements;
sb.append("[");
for (Expr node : e) {
sb.append(node);
sb.append(" ");
}
sb.append("]");
return sb.toString();
}
}
package interpreter.lisp;
import java.util.ArrayList;
import java.util.List;
public class CompoundFunction extends CompoundExpression implements Fun {
private List<Symbol> parameters;
private Symbol name;
private Expr body;
public CompoundFunction(Expr name, Expr parameters, Expr body) {
this.name = (Symbol) name;
List<Expr> elts = ((CompoundExpression) parameters).getElements();
List<Symbol> paramNames = new ArrayList<Symbol>();
for (Expr node : elts) {
paramNames.add((Symbol) node);
}
this.parameters = paramNames;
this.body = body;
}
/* (non-Javadoc)
* @see interpreter.lisp.Fun#getParameters()
*/
@Override
public List<Symbol> getParameters() {
return parameters;
}
public Symbol getName() {
return name;
}
/* (non-Javadoc)
* @see interpreter.lisp.Fun#getBody()
*/
@Override
public Expr getBody() {
return body;
}
@Override
public Expr evaluate(Context context) {
context.put(getName(),this);
return this;
}
@Override
public String toString() {
return getBody().toString();
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment