-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Began drafting support for binary object layout
- Loading branch information
1 parent
e6351a1
commit 5526917
Showing
4 changed files
with
125 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
49 changes: 49 additions & 0 deletions
49
Lang/src/main/java/chipmunk/compiler/ast/transforms/TypeBuilderVisitor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters