Skip to content

Commit

Permalink
Began drafting support for binary object layout
Browse files Browse the repository at this point in the history
  • Loading branch information
danielperano committed Oct 4, 2024
1 parent e6351a1 commit 5526917
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 3 deletions.
6 changes: 3 additions & 3 deletions Lang/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

plugins {
id 'maven-publish'
id 'org.asciidoctor.jvm.convert' version '3.1.0'
//id 'org.asciidoctor.jvm.convert' version '3.1.0'
}

apply plugin: 'java'
Expand Down Expand Up @@ -119,9 +119,9 @@ publishing {
}
}

asciidoctor {
/*asciidoctor {
sourceDir 'src/doc/asciidoc'
}
}*/

clean {
delete 'build'
Expand Down
69 changes: 69 additions & 0 deletions Lang/src/main/java/chipmunk/compiler/Layout.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright (C) 2024 MyWorld, LLC
* All rights reserved.
*
* This file is part of Chipmunk.
*
* Chipmunk is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Chipmunk is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Chipmunk. If not, see <https://www.gnu.org/licenses/>.
*/

package chipmunk.compiler;

import chipmunk.compiler.types.ObjectType;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

public class Layout {

public static final int WORD_SIZE_STANDARD = 1;
public static final int WORD_SIZE_DOUBLE = 2;

public record Field(String name, ObjectType type, int size){}

protected final String name;
protected final ObjectType type;
protected final List<Field> fields;

public Layout(String name, ObjectType type){
this.name = name;
this.type = type;
fields = new ArrayList<>();
}

public boolean hasField(String name){
return fields.stream()
.anyMatch(f -> f.name().equals(name));
}

public Layout addField(Field field){
if(hasField(field.name())){
throw new IllegalArgumentException("Field " + field.name() + " already exists for layout " + name);
}
fields.add(field);
return this;
}

public List<Field> getFields(){
return fields;
}

public Optional<Field> getField(String name){
return fields.stream()
.filter(field -> field.name().equals(name))
.findFirst();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright (C) 2024 MyWorld, LLC
* All rights reserved.
*
* This file is part of Chipmunk.
*
* Chipmunk is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Chipmunk is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Chipmunk. If not, see <https://www.gnu.org/licenses/>.
*/

package chipmunk.compiler.ast.transforms;

import chipmunk.compiler.ast.AstNode;
import chipmunk.compiler.ast.AstVisitor;
import chipmunk.compiler.lexer.TokenType;
import chipmunk.compiler.types.ObjectType;

public class TypeBuilderVisitor implements AstVisitor {

@Override
public void visit(AstNode node) {
node.visitChildren(this);

switch (node.getNodeType()){
case LITERAL -> {
if(node.getToken().type() == TokenType.INTLITERAL){
node.setResultType(ObjectType.INT);
}else if(node.getToken().type() == TokenType.FLOATLITERAL){
node.setResultType(ObjectType.FLOAT);
}
}
case OPERATOR -> {
if(node.getToken().type() == TokenType.PLUS){
node.setResultType(ObjectType.INT); // TODO - match off of children
}
}
}
}
}
4 changes: 4 additions & 0 deletions Lang/src/main/java/chipmunk/compiler/types/ObjectType.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
public record ObjectType(String name, boolean isPrimitive, boolean isType, List<ObjectType> superTypes) {

public static final ObjectType ANY = new ObjectType("Any", false, true);
public static final ObjectType INT = new ObjectType("Int", true, false);
public static final ObjectType FLOAT = new ObjectType("Float", true, false);
public static final ObjectType LONG = new ObjectType("Long", true, false);
public static final ObjectType DOUBLE = new ObjectType("Double", true, false);

public ObjectType(String name, boolean isPrimitive, boolean isType){
this(name, isPrimitive, isType, List.of());
Expand Down

0 comments on commit 5526917

Please sign in to comment.