Compare commits
No commits in common. "main" and "lsp" have entirely different histories.
@ -71,8 +71,8 @@ Currently missing big features (TODOs) are:
|
|||||||
Big features that I want later but are not necessary:
|
Big features that I want later but are not necessary:
|
||||||
- ~~User-defined binary operations~~ (DONE)
|
- ~~User-defined binary operations~~ (DONE)
|
||||||
- ~~Asymmetric binary operations (e.g. string + u32)~~ (DONE)
|
- ~~Asymmetric binary operations (e.g. string + u32)~~ (DONE)
|
||||||
- ~~Error handling~~ (Not Doing It)
|
- Error handling
|
||||||
- ~~Lexing & parsing of whitespace and comments as well~~ (DONE)
|
- Lexing & parsing of whitespace and comments as well
|
||||||
- LSP implementation
|
- LSP implementation
|
||||||
|
|
||||||
Smaller features:
|
Smaller features:
|
||||||
|
@ -1,13 +1,12 @@
|
|||||||
a
|
// Main
|
||||||
(
|
fn main() -> bool {
|
||||||
b
|
return 144 == fibonacci(0xc);
|
||||||
)
|
}
|
||||||
x
|
|
||||||
(
|
// Fibonacci
|
||||||
(
|
fn fibonacci(value: u16) -> u16 {
|
||||||
c
|
if value <= 2 {
|
||||||
xyz
|
return 1;
|
||||||
)
|
}
|
||||||
)
|
fibonacci(value - 1) + fibonacci(value - 2)
|
||||||
(
|
}
|
||||||
a
|
|
||||||
|
@ -4,6 +4,5 @@ import std::print;
|
|||||||
fn main() -> u8 {
|
fn main() -> u8 {
|
||||||
let bytes = include_bytes!("./macro_easy_file.txt");
|
let bytes = include_bytes!("./macro_easy_file.txt");
|
||||||
print(String::new() + bytes.length());
|
print(String::new() + bytes.length());
|
||||||
print(String::new() + (include_bytes!("./macro_easy_file.txt") as *u8)[1] as u64);
|
return (bytes as *u8)[0];
|
||||||
return (include_bytes!("./macro_easy_file.txt") as *u8)[0];
|
|
||||||
}
|
}
|
||||||
|
@ -610,9 +610,30 @@ impl Builder {
|
|||||||
Instr::PtrToInt(instr, ty) => instr.cast_to(self, &ty).map(|_| ()),
|
Instr::PtrToInt(instr, ty) => instr.cast_to(self, &ty).map(|_| ()),
|
||||||
Instr::IntToPtr(instr, ty) => instr.cast_to(self, &ty).map(|_| ()),
|
Instr::IntToPtr(instr, ty) => instr.cast_to(self, &ty).map(|_| ()),
|
||||||
Instr::BitCast(..) => Ok(()),
|
Instr::BitCast(..) => Ok(()),
|
||||||
Instr::ShiftRightLogical(lhs, rhs) => match_types(&lhs, &rhs, &self).map(|_| ()),
|
Instr::ShiftRightLogical(_, rhs) => {
|
||||||
Instr::ShiftRightArithmetic(lhs, rhs) => match_types(&lhs, &rhs, &self).map(|_| ()),
|
let rhs_ty = rhs.get_type(&self)?;
|
||||||
Instr::ShiftLeft(lhs, rhs) => match_types(&lhs, &rhs, &self).map(|_| ()),
|
if rhs_ty.category() == TypeCategory::UnsignedInteger {
|
||||||
|
Ok(())
|
||||||
|
} else {
|
||||||
|
Err(ErrorKind::Null)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Instr::ShiftRightArithmetic(_, rhs) => {
|
||||||
|
let rhs_ty = rhs.get_type(&self)?;
|
||||||
|
if rhs_ty.category() == TypeCategory::UnsignedInteger {
|
||||||
|
Ok(())
|
||||||
|
} else {
|
||||||
|
Err(ErrorKind::Null)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Instr::ShiftLeft(_, rhs) => {
|
||||||
|
let rhs_ty = rhs.get_type(&self)?;
|
||||||
|
if rhs_ty.category() == TypeCategory::UnsignedInteger {
|
||||||
|
Ok(())
|
||||||
|
} else {
|
||||||
|
Err(ErrorKind::Null)
|
||||||
|
}
|
||||||
|
}
|
||||||
Instr::GetGlobal(_) => Ok(()),
|
Instr::GetGlobal(_) => Ok(()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -540,7 +540,7 @@ impl DebugTypeHolder {
|
|||||||
field.pos.map(|p| p.line).unwrap_or(1),
|
field.pos.map(|p| p.line).unwrap_or(1),
|
||||||
field.size_bits,
|
field.size_bits,
|
||||||
0,
|
0,
|
||||||
field.offset,
|
1,
|
||||||
field.flags.as_llvm(),
|
field.flags.as_llvm(),
|
||||||
*debug.types.get(&field.ty).unwrap(),
|
*debug.types.get(&field.ty).unwrap(),
|
||||||
)
|
)
|
||||||
|
@ -43,18 +43,11 @@
|
|||||||
"description": "Traces the communication between VS Code and the language server."
|
"description": "Traces the communication between VS Code and the language server."
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
"grammars": [
|
|
||||||
{
|
|
||||||
"language": "reid",
|
|
||||||
"scopeName": "source.reid",
|
|
||||||
"path": "./syntaxes/grammar.json"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"vscode:prepublish": "pnpm run package",
|
"vscode:prepublish": "pnpm run package",
|
||||||
"compile": "npx js-yaml syntaxes/grammar.yaml > syntaxes/grammar.json && webpack",
|
"compile": "webpack",
|
||||||
"watch": "webpack --watch",
|
"watch": "webpack --watch",
|
||||||
"package": "webpack --mode production --devtool hidden-source-map",
|
"package": "webpack --mode production --devtool hidden-source-map",
|
||||||
"compile-tests": "tsc -p . --outDir out",
|
"compile-tests": "tsc -p . --outDir out",
|
||||||
@ -72,7 +65,6 @@
|
|||||||
"@vscode/test-cli": "^0.0.11",
|
"@vscode/test-cli": "^0.0.11",
|
||||||
"@vscode/test-electron": "^2.5.2",
|
"@vscode/test-electron": "^2.5.2",
|
||||||
"eslint": "^9.25.1",
|
"eslint": "^9.25.1",
|
||||||
"js-yaml": "^4.1.0",
|
|
||||||
"ts-loader": "^9.5.2",
|
"ts-loader": "^9.5.2",
|
||||||
"typescript": "^5.8.3",
|
"typescript": "^5.8.3",
|
||||||
"webpack": "^5.99.7",
|
"webpack": "^5.99.7",
|
||||||
|
@ -12,9 +12,9 @@ use reid::{compile_module, parse_module, perform_all_passes};
|
|||||||
use tower_lsp::lsp_types::{
|
use tower_lsp::lsp_types::{
|
||||||
self, CompletionItem, CompletionOptions, CompletionParams, CompletionResponse, Diagnostic, DiagnosticSeverity,
|
self, CompletionItem, CompletionOptions, CompletionParams, CompletionResponse, Diagnostic, DiagnosticSeverity,
|
||||||
DidChangeTextDocumentParams, DidOpenTextDocumentParams, Hover, HoverContents, HoverParams, HoverProviderCapability,
|
DidChangeTextDocumentParams, DidOpenTextDocumentParams, Hover, HoverContents, HoverParams, HoverProviderCapability,
|
||||||
InitializeParams, InitializeResult, InitializedParams, MarkedString, MarkupContent, MarkupKind, MessageType, OneOf,
|
InitializeParams, InitializeResult, InitializedParams, MarkedString, MessageType, OneOf, Range, ServerCapabilities,
|
||||||
Range, ServerCapabilities, TextDocumentItem, TextDocumentSyncCapability, TextDocumentSyncKind,
|
TextDocumentItem, TextDocumentSyncCapability, TextDocumentSyncKind, TextDocumentSyncOptions,
|
||||||
TextDocumentSyncOptions, WorkspaceFoldersServerCapabilities, WorkspaceServerCapabilities,
|
WorkspaceFoldersServerCapabilities, WorkspaceServerCapabilities,
|
||||||
};
|
};
|
||||||
use tower_lsp::{Client, LanguageServer, LspService, Server, jsonrpc};
|
use tower_lsp::{Client, LanguageServer, LspService, Server, jsonrpc};
|
||||||
|
|
||||||
@ -91,38 +91,24 @@ impl LanguageServer for Backend {
|
|||||||
None
|
None
|
||||||
};
|
};
|
||||||
|
|
||||||
let (range, ty) = if let Some(token) = token {
|
let ty = if let Some(token) = token {
|
||||||
if let Some(possible_ty) = self.types.get(&file_name).unwrap().get(token) {
|
if let Some(possible_ty) = self.types.get(&file_name).unwrap().get(token) {
|
||||||
let start = token.position;
|
|
||||||
let end = token.position.add(token.token.len() as u32);
|
|
||||||
let range = Range {
|
|
||||||
start: lsp_types::Position {
|
|
||||||
line: (start.1 as i32 - 1).max(0) as u32,
|
|
||||||
character: (start.0 as i32 - 1).max(0) as u32,
|
|
||||||
},
|
|
||||||
end: lsp_types::Position {
|
|
||||||
line: (end.1 as i32 - 1).max(0) as u32,
|
|
||||||
character: (end.0 as i32 - 1).max(0) as u32,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
if let Some(ty) = possible_ty.clone() {
|
if let Some(ty) = possible_ty.clone() {
|
||||||
(Some(range), format!("{}", ty))
|
format!("{}", ty)
|
||||||
} else {
|
} else {
|
||||||
(Some(range), String::from("no type"))
|
String::from("no type")
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
(None, String::from("no token"))
|
String::from("no token")
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
(None, String::from("no token"))
|
String::from("no token")
|
||||||
};
|
};
|
||||||
|
|
||||||
let contents = HoverContents::Markup(MarkupContent {
|
Ok(Some(Hover {
|
||||||
kind: MarkupKind::Markdown,
|
contents: HoverContents::Scalar(MarkedString::String(format!("{}", ty))),
|
||||||
value: format!("`{ty}`"),
|
range: None,
|
||||||
});
|
}))
|
||||||
|
|
||||||
Ok(Some(Hover { contents, range }))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn did_open(&self, params: DidOpenTextDocumentParams) {
|
async fn did_open(&self, params: DidOpenTextDocumentParams) {
|
||||||
@ -286,40 +272,6 @@ pub fn find_type_in_context(module: &mir::Module, token_idx: usize) -> Option<Ty
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for binop in &module.binop_defs {
|
|
||||||
if let Some(meta) = binop.block_meta() {
|
|
||||||
if !meta.contains(token_idx) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
return match &binop.fn_kind {
|
|
||||||
mir::FunctionDefinitionKind::Local(block, _) => find_type_in_block(&block, module.module_id, token_idx),
|
|
||||||
mir::FunctionDefinitionKind::Extern(_) => None,
|
|
||||||
mir::FunctionDefinitionKind::Intrinsic(_) => None,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
for (_, function) in &module.associated_functions {
|
|
||||||
if !(function.signature() + function.block_meta()).contains(token_idx) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
for param in &function.parameters {
|
|
||||||
if param.meta.contains(token_idx) {
|
|
||||||
return Some(param.ty.clone());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return match &function.kind {
|
|
||||||
mir::FunctionDefinitionKind::Local(block, _) => find_type_in_block(&block, module.module_id, token_idx),
|
|
||||||
mir::FunctionDefinitionKind::Extern(_) => None,
|
|
||||||
mir::FunctionDefinitionKind::Intrinsic(_) => None,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
for function in &module.functions {
|
for function in &module.functions {
|
||||||
if !(function.signature() + function.block_meta()).contains(token_idx) {
|
if !(function.signature() + function.block_meta()).contains(token_idx) {
|
||||||
continue;
|
continue;
|
||||||
@ -420,7 +372,7 @@ pub fn find_type_in_expr(expr: &mir::Expression, module_id: SourceModuleId, toke
|
|||||||
Some(TypeKind::CustomType(mir::CustomTypeKey(name.clone(), module_id)))
|
Some(TypeKind::CustomType(mir::CustomTypeKey(name.clone(), module_id)))
|
||||||
}
|
}
|
||||||
mir::ExprKind::Literal(literal) => Some(literal.as_type()),
|
mir::ExprKind::Literal(literal) => Some(literal.as_type()),
|
||||||
mir::ExprKind::BinOp(_, lhs, rhs, type_kind) => {
|
mir::ExprKind::BinOp(binary_operator, lhs, rhs, type_kind) => {
|
||||||
if let Some(ty) = find_type_in_expr(lhs, module_id, token_idx) {
|
if let Some(ty) = find_type_in_expr(lhs, module_id, token_idx) {
|
||||||
return Some(ty);
|
return Some(ty);
|
||||||
}
|
}
|
||||||
|
@ -1,395 +0,0 @@
|
|||||||
{
|
|
||||||
"scopeName": "source.reid",
|
|
||||||
"patterns": [
|
|
||||||
{
|
|
||||||
"include": "#import"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"include": "#expression"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"repository": {
|
|
||||||
"import": {
|
|
||||||
"begin": "(import)\\s*",
|
|
||||||
"end": ";",
|
|
||||||
"beginCaptures": {
|
|
||||||
"1": {
|
|
||||||
"name": "keyword"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"endCaptures": {
|
|
||||||
"0": {
|
|
||||||
"name": "punctuation.semi.reid"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"patterns": [
|
|
||||||
{
|
|
||||||
"include": "#identifier"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"include": "#punctiation"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"punctuation": {
|
|
||||||
"patterns": [
|
|
||||||
{
|
|
||||||
"match": "::",
|
|
||||||
"name": "keyword.operator.namespace.reid"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"match": ";",
|
|
||||||
"name": "punctuation.semi.reid"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"match": ".",
|
|
||||||
"name": "punctuation.dot.reid"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"match": ",",
|
|
||||||
"name": "punctuation.comma.reid"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"expression": {
|
|
||||||
"patterns": [
|
|
||||||
{
|
|
||||||
"include": "#comment"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"include": "#fn-signature"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"include": "#common-type"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"include": "#binop-impl"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"include": "#type-impl"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"include": "#struct-definition"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"include": "#block"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"include": "#binop"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"include": "#namespace"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"include": "#cast"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"include": "#function-call"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"include": "#parenthesis"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"include": "#array"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"include": "#keywords"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"include": "#struct-expression"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"include": "#number-literal"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"include": "#string-literal"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"include": "#identifier"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"include": "#punctuation"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"comment": {
|
|
||||||
"match": "\\/\\/(.|\\/)*",
|
|
||||||
"name": "comment.line.double-slash.reid"
|
|
||||||
},
|
|
||||||
"fn-signature": {
|
|
||||||
"begin": "(fn)\\s*(\\w+)\\(",
|
|
||||||
"beginCaptures": {
|
|
||||||
"1": {
|
|
||||||
"name": "keyword.fn.reid"
|
|
||||||
},
|
|
||||||
"2": {
|
|
||||||
"name": "entity.name.function.reid"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"end": "\\)",
|
|
||||||
"patterns": [
|
|
||||||
{
|
|
||||||
"include": "#annotated-identifier"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"include": "#keywords"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"include": "#binop"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"endCaptures": {
|
|
||||||
"2": {
|
|
||||||
"name": "entity.name.type.reid"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"type-impl": {
|
|
||||||
"begin": "(impl)\\s* (\\w+)\\s* \\{\n",
|
|
||||||
"end": "\\}",
|
|
||||||
"captures": {
|
|
||||||
"1": {
|
|
||||||
"name": "keyword.impl.reid"
|
|
||||||
},
|
|
||||||
"2": {
|
|
||||||
"name": "entity.name.type"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"patterns": [
|
|
||||||
{
|
|
||||||
"include": "#expression"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"binop-impl": {
|
|
||||||
"begin": "(impl)\\s+(binop)\\s+\\(((.*)\\s*:\\s*(.*))\\)(.*)\\(((.*)\\s*:\\s*(.*))\\)\\s*->\\s*(\\w+)\\s*\\{",
|
|
||||||
"end": "\\}",
|
|
||||||
"beginCaptures": {
|
|
||||||
"1": {
|
|
||||||
"name": "keyword.impl.reid"
|
|
||||||
},
|
|
||||||
"2": {
|
|
||||||
"name": "keyword.impl.reid"
|
|
||||||
},
|
|
||||||
"4": {
|
|
||||||
"name": "variable.parameter.binop.reid"
|
|
||||||
},
|
|
||||||
"5": {
|
|
||||||
"name": "entity.name.type.parameter.binop.reid"
|
|
||||||
},
|
|
||||||
"6": {
|
|
||||||
"name": "keyword.operator.math.reid"
|
|
||||||
},
|
|
||||||
"8": {
|
|
||||||
"name": "variable.parameter.binop.reid"
|
|
||||||
},
|
|
||||||
"9": {
|
|
||||||
"name": "entity.name.type.parameter.binop.reid"
|
|
||||||
},
|
|
||||||
"10": {
|
|
||||||
"name": "entity.name.type.return.binop.reid"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"patterns": [
|
|
||||||
{
|
|
||||||
"include": "#expression"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"struct-definition": {
|
|
||||||
"begin": "(struct)\\s*(\\w+)\\s*\\{",
|
|
||||||
"end": "\\}",
|
|
||||||
"captures": {
|
|
||||||
"1": {
|
|
||||||
"name": "keyword.struct.reid"
|
|
||||||
},
|
|
||||||
"2": {
|
|
||||||
"name": "entity.name.type"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"patterns": [
|
|
||||||
{
|
|
||||||
"include": "#annotated-identifier"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"struct-expression": {
|
|
||||||
"begin": "([A-Z]\\w*)\\s*\\{",
|
|
||||||
"end": "\\}",
|
|
||||||
"captures": {
|
|
||||||
"1": {
|
|
||||||
"name": "entity.name.type.struct.reid"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"patterns": [
|
|
||||||
{
|
|
||||||
"include": "#expression"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"number-literal": {
|
|
||||||
"patterns": [
|
|
||||||
{
|
|
||||||
"match": "0x[0-9a-fA-F]+(\\.[0-9a-fA-F]+)?",
|
|
||||||
"name": "constant.hexadecimal"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"match": "0o[0-7]+(\\.[0-7]+)?",
|
|
||||||
"name": "constant.octal"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"match": "0b[01]+(\\.[01]+)?",
|
|
||||||
"name": "constant.binary"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"match": "[0-9]+(\\.[0-9]+)?",
|
|
||||||
"name": "constant.numeric"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"string-literal": {
|
|
||||||
"begin": "\"",
|
|
||||||
"end": "\"",
|
|
||||||
"name": "string.quoted.double",
|
|
||||||
"patterns": [
|
|
||||||
{
|
|
||||||
"match": "\\.",
|
|
||||||
"name": "constant.character.escape"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"block": {
|
|
||||||
"begin": "\\{",
|
|
||||||
"end": "\\}",
|
|
||||||
"patterns": [
|
|
||||||
{
|
|
||||||
"include": "#expression"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"namespace": {
|
|
||||||
"match": "(\\w+)(\\:\\:)",
|
|
||||||
"captures": {
|
|
||||||
"1": {
|
|
||||||
"name": "entity.name.function.reid"
|
|
||||||
},
|
|
||||||
"2": {
|
|
||||||
"name": "keyword.operator.namespace.reid"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"cast": {
|
|
||||||
"match": "(as)\\s+(\\w+)",
|
|
||||||
"captures": {
|
|
||||||
"1": {
|
|
||||||
"name": "keyword.cast.reid"
|
|
||||||
},
|
|
||||||
"2": {
|
|
||||||
"name": "entity.name.type.reid"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"function-call": {
|
|
||||||
"begin": "(\\w+)?\\(",
|
|
||||||
"end": "\\)",
|
|
||||||
"beginCaptures": {
|
|
||||||
"1": {
|
|
||||||
"name": "entity.name.function.reid"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"patterns": [
|
|
||||||
{
|
|
||||||
"include": "#expression"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"parenthesis": {
|
|
||||||
"begin": "\\(",
|
|
||||||
"end": "\\)",
|
|
||||||
"beginCaptures": {
|
|
||||||
"0": {
|
|
||||||
"name": "keyword.operator.parenthesis.reid"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"endCaptures": {
|
|
||||||
"0": {
|
|
||||||
"name": "keyword.operator.parenthesis.reid"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"patterns": [
|
|
||||||
{
|
|
||||||
"include": "#expression"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"annotated-identifier": {
|
|
||||||
"begin": "(\\w+)\\:",
|
|
||||||
"end": ",",
|
|
||||||
"beginCaptures": {
|
|
||||||
"1": {
|
|
||||||
"name": "variable.language.reid"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"patterns": [
|
|
||||||
{
|
|
||||||
"include": "#expression"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"identifier": {
|
|
||||||
"patterns": [
|
|
||||||
{
|
|
||||||
"match": "[A-Z]\\w*",
|
|
||||||
"name": "entity.name.type.reid"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"match": "\\w+",
|
|
||||||
"name": "variable.language.reid"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"keywords": {
|
|
||||||
"patterns": [
|
|
||||||
{
|
|
||||||
"match": "let|mut|pub|extern",
|
|
||||||
"name": "storage.type.reid"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"match": "if|return",
|
|
||||||
"name": "keyword.control"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"match": "self",
|
|
||||||
"name": "variable.language.self.reid"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"binop": {
|
|
||||||
"match": "\\<\\=|\\>\\=|\\=\\=|\\<|\\>|\\*|\\+|\\-|\\^|\\&\\&|\\&",
|
|
||||||
"name": "keyword.operator.math.reid"
|
|
||||||
},
|
|
||||||
"array": {
|
|
||||||
"begin": "\\[",
|
|
||||||
"end": "\\]",
|
|
||||||
"beginCaptures": {
|
|
||||||
"0": {
|
|
||||||
"name": "entity.name.type.array.reid"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"endCaptures": {
|
|
||||||
"0": {
|
|
||||||
"name": "entity.name.type.array.reid"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"patterns": [
|
|
||||||
{
|
|
||||||
"include": "#expression"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"common-type": {
|
|
||||||
"match": "u8|u16|u32|u64|u128|i8|i16|i32|i64|i128|bool",
|
|
||||||
"name": "entity.name.type.common.reid"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,232 +0,0 @@
|
|||||||
scopeName: source.reid
|
|
||||||
patterns:
|
|
||||||
- include: "#import"
|
|
||||||
- include: "#expression"
|
|
||||||
repository:
|
|
||||||
# function-definition:
|
|
||||||
# begin: "(fn)\\s*(\\w+)\\(((\\w+)\\s*\\:\\s*(\\w+),?)*\\)\\s*->\\s*(\\w+)\\s*\\{"
|
|
||||||
# end: "\\}"
|
|
||||||
# beginCaptures:
|
|
||||||
# 1:
|
|
||||||
# name: "keyword.other"
|
|
||||||
# 2:
|
|
||||||
# name: "entity.name.function"
|
|
||||||
# 4:
|
|
||||||
# name: "entity.name.parameter"
|
|
||||||
# 5:
|
|
||||||
# name: "entity.name.type"
|
|
||||||
# 6:
|
|
||||||
# name: "entity.name.type"
|
|
||||||
# patterns:
|
|
||||||
# - include: "#type"
|
|
||||||
# - include: "#expression"
|
|
||||||
import:
|
|
||||||
begin: "(import)\\s*"
|
|
||||||
end: ";"
|
|
||||||
beginCaptures:
|
|
||||||
1:
|
|
||||||
name: keyword
|
|
||||||
endCaptures:
|
|
||||||
0:
|
|
||||||
name: punctuation.semi.reid
|
|
||||||
patterns:
|
|
||||||
- include: "#identifier"
|
|
||||||
- include: "#punctiation"
|
|
||||||
punctuation:
|
|
||||||
patterns:
|
|
||||||
- match: "::"
|
|
||||||
name: keyword.operator.namespace.reid
|
|
||||||
- match: ";"
|
|
||||||
name: punctuation.semi.reid
|
|
||||||
- match: "."
|
|
||||||
name: punctuation.dot.reid
|
|
||||||
- match: ","
|
|
||||||
name: punctuation.comma.reid
|
|
||||||
expression:
|
|
||||||
patterns:
|
|
||||||
- include: "#comment"
|
|
||||||
- include: "#fn-signature"
|
|
||||||
- include: "#common-type"
|
|
||||||
- include: "#binop-impl"
|
|
||||||
- include: "#type-impl"
|
|
||||||
- include: "#struct-definition"
|
|
||||||
- include: "#block"
|
|
||||||
- include: "#binop"
|
|
||||||
- include: "#namespace"
|
|
||||||
- include: "#cast"
|
|
||||||
- include: "#function-call"
|
|
||||||
- include: "#parenthesis"
|
|
||||||
- include: "#array"
|
|
||||||
- include: "#keywords"
|
|
||||||
- include: "#struct-expression"
|
|
||||||
- include: "#number-literal"
|
|
||||||
- include: "#string-literal"
|
|
||||||
- include: "#identifier"
|
|
||||||
- include: "#punctuation"
|
|
||||||
comment:
|
|
||||||
match: "\\/\\/(.|\\/)*"
|
|
||||||
name: comment.line.double-slash.reid
|
|
||||||
fn-signature:
|
|
||||||
begin: "(fn)\\s*(\\w+)\\("
|
|
||||||
beginCaptures:
|
|
||||||
1:
|
|
||||||
name: keyword.fn.reid
|
|
||||||
2:
|
|
||||||
name: entity.name.function.reid
|
|
||||||
end: "\\)"
|
|
||||||
patterns:
|
|
||||||
- include: "#annotated-identifier"
|
|
||||||
- include: "#keywords"
|
|
||||||
- include: "#binop"
|
|
||||||
endCaptures:
|
|
||||||
2:
|
|
||||||
name: entity.name.type.reid
|
|
||||||
type-impl:
|
|
||||||
begin: >
|
|
||||||
(impl)\s*
|
|
||||||
(\w+)\s*
|
|
||||||
\{
|
|
||||||
end: "\\}"
|
|
||||||
captures:
|
|
||||||
1:
|
|
||||||
name: keyword.impl.reid
|
|
||||||
2:
|
|
||||||
name: entity.name.type
|
|
||||||
patterns:
|
|
||||||
- include: "#expression"
|
|
||||||
binop-impl:
|
|
||||||
begin: "(impl)\\s+(binop)\\s+\\(((.*)\\s*:\\s*(.*))\\)(.*)\\(((.*)\\s*:\\s*(.*))\\)\\s*->\\s*(\\w+)\\s*\\{"
|
|
||||||
end: "\\}"
|
|
||||||
beginCaptures:
|
|
||||||
1:
|
|
||||||
name: keyword.impl.reid
|
|
||||||
2:
|
|
||||||
name: keyword.impl.reid
|
|
||||||
4:
|
|
||||||
name: variable.parameter.binop.reid
|
|
||||||
5:
|
|
||||||
name: entity.name.type.parameter.binop.reid
|
|
||||||
6:
|
|
||||||
name: keyword.operator.math.reid
|
|
||||||
8:
|
|
||||||
name: variable.parameter.binop.reid
|
|
||||||
9:
|
|
||||||
name: entity.name.type.parameter.binop.reid
|
|
||||||
10:
|
|
||||||
name: entity.name.type.return.binop.reid
|
|
||||||
patterns:
|
|
||||||
- include: "#expression"
|
|
||||||
struct-definition:
|
|
||||||
begin: "(struct)\\s*(\\w+)\\s*\\{"
|
|
||||||
end: "\\}"
|
|
||||||
captures:
|
|
||||||
1:
|
|
||||||
name: keyword.struct.reid
|
|
||||||
2:
|
|
||||||
name: entity.name.type
|
|
||||||
patterns:
|
|
||||||
- include: "#annotated-identifier"
|
|
||||||
struct-expression:
|
|
||||||
begin: "([A-Z]\\w*)\\s*\\{"
|
|
||||||
end: "\\}"
|
|
||||||
captures:
|
|
||||||
1:
|
|
||||||
name: entity.name.type.struct.reid
|
|
||||||
patterns:
|
|
||||||
- include: "#expression"
|
|
||||||
number-literal:
|
|
||||||
patterns:
|
|
||||||
- match: "0x[0-9a-fA-F]+(\\.[0-9a-fA-F]+)?"
|
|
||||||
name: "constant.hexadecimal"
|
|
||||||
- match: "0o[0-7]+(\\.[0-7]+)?"
|
|
||||||
name: "constant.octal"
|
|
||||||
- match: "0b[01]+(\\.[01]+)?"
|
|
||||||
name: "constant.binary"
|
|
||||||
- match: "[0-9]+(\\.[0-9]+)?"
|
|
||||||
name: "constant.numeric"
|
|
||||||
string-literal:
|
|
||||||
begin: '"'
|
|
||||||
end: '"'
|
|
||||||
name: string.quoted.double
|
|
||||||
patterns:
|
|
||||||
- match: "\\."
|
|
||||||
name: constant.character.escape
|
|
||||||
block:
|
|
||||||
begin: "\\{"
|
|
||||||
end: "\\}"
|
|
||||||
patterns:
|
|
||||||
- include: "#expression"
|
|
||||||
namespace:
|
|
||||||
match: "(\\w+)(\\:\\:)"
|
|
||||||
captures:
|
|
||||||
1:
|
|
||||||
name: entity.name.function.reid
|
|
||||||
2:
|
|
||||||
name: keyword.operator.namespace.reid
|
|
||||||
cast:
|
|
||||||
match: "(as)\\s+(\\w+)"
|
|
||||||
captures:
|
|
||||||
1:
|
|
||||||
name: keyword.cast.reid
|
|
||||||
2:
|
|
||||||
name: entity.name.type.reid
|
|
||||||
function-call:
|
|
||||||
begin: "(\\w+)?\\("
|
|
||||||
end: "\\)"
|
|
||||||
beginCaptures:
|
|
||||||
1:
|
|
||||||
name: entity.name.function.reid
|
|
||||||
patterns:
|
|
||||||
- include: "#expression"
|
|
||||||
parenthesis:
|
|
||||||
begin: "\\("
|
|
||||||
end: "\\)"
|
|
||||||
beginCaptures:
|
|
||||||
0:
|
|
||||||
name: keyword.operator.parenthesis.reid
|
|
||||||
endCaptures:
|
|
||||||
0:
|
|
||||||
name: keyword.operator.parenthesis.reid
|
|
||||||
patterns:
|
|
||||||
- include: "#expression"
|
|
||||||
annotated-identifier:
|
|
||||||
begin: "(\\w+)\\:"
|
|
||||||
end: ","
|
|
||||||
beginCaptures:
|
|
||||||
1:
|
|
||||||
name: variable.language.reid
|
|
||||||
patterns:
|
|
||||||
- include: "#expression"
|
|
||||||
identifier:
|
|
||||||
patterns:
|
|
||||||
- match: "[A-Z]\\w*"
|
|
||||||
name: entity.name.type.reid
|
|
||||||
- match: "\\w+"
|
|
||||||
name: variable.language.reid
|
|
||||||
keywords:
|
|
||||||
patterns:
|
|
||||||
- match: "let|mut|pub|extern"
|
|
||||||
name: "storage.type.reid"
|
|
||||||
- match: "if|return"
|
|
||||||
name: "keyword.control"
|
|
||||||
- match: "self"
|
|
||||||
name: "variable.language.self.reid"
|
|
||||||
binop:
|
|
||||||
match: "\\<\\=|\\>\\=|\\=\\=|\\<|\\>|\\*|\\+|\\-|\\^|\\&\\&|\\&"
|
|
||||||
name: keyword.operator.math.reid
|
|
||||||
array:
|
|
||||||
begin: "\\["
|
|
||||||
end: "\\]"
|
|
||||||
beginCaptures:
|
|
||||||
0:
|
|
||||||
name: entity.name.type.array.reid
|
|
||||||
endCaptures:
|
|
||||||
0:
|
|
||||||
name: entity.name.type.array.reid
|
|
||||||
patterns:
|
|
||||||
- include: "#expression"
|
|
||||||
common-type:
|
|
||||||
match: "u8|u16|u32|u64|u128|i8|i16|i32|i64|i128|bool"
|
|
||||||
name: entity.name.type.common.reid
|
|
||||||
|
|
||||||
|
|
@ -74,7 +74,7 @@ pub fn get_intrinsic_assoc_func(ty: &TypeKind, name: &str) -> Option<FunctionDef
|
|||||||
}],
|
}],
|
||||||
kind: FunctionDefinitionKind::Intrinsic(Box::new(IntrinsicConst(*len))),
|
kind: FunctionDefinitionKind::Intrinsic(Box::new(IntrinsicConst(*len))),
|
||||||
source: None,
|
source: None,
|
||||||
});
|
})
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
@ -247,17 +247,26 @@ pub fn form_intrinsic_binops() -> Vec<BinopDefinition> {
|
|||||||
scope.block.build(Instr::XOr(lhs, rhs)).unwrap()
|
scope.block.build(Instr::XOr(lhs, rhs)).unwrap()
|
||||||
}));
|
}));
|
||||||
if ty.signed() {
|
if ty.signed() {
|
||||||
intrinsics.push(complex_binop_def(BitshiftRight, &ty, &ty, |scope, lhs, rhs| {
|
intrinsics.push(complex_binop_def(
|
||||||
scope.block.build(Instr::ShiftRightArithmetic(lhs, rhs)).unwrap()
|
BitshiftRight,
|
||||||
}));
|
&ty,
|
||||||
|
&TypeKind::U64,
|
||||||
|
|scope, lhs, rhs| scope.block.build(Instr::ShiftRightArithmetic(lhs, rhs)).unwrap(),
|
||||||
|
));
|
||||||
} else {
|
} else {
|
||||||
intrinsics.push(complex_binop_def(BitshiftRight, &ty, &ty, |scope, lhs, rhs| {
|
intrinsics.push(complex_binop_def(
|
||||||
scope.block.build(Instr::ShiftRightLogical(lhs, rhs)).unwrap()
|
BitshiftRight,
|
||||||
}));
|
&ty,
|
||||||
|
&TypeKind::U64,
|
||||||
|
|scope, lhs, rhs| scope.block.build(Instr::ShiftRightLogical(lhs, rhs)).unwrap(),
|
||||||
|
));
|
||||||
}
|
}
|
||||||
intrinsics.push(complex_binop_def(BitshiftLeft, &ty, &ty, |scope, lhs, rhs| {
|
intrinsics.push(complex_binop_def(
|
||||||
scope.block.build(Instr::ShiftLeft(lhs, rhs)).unwrap()
|
BitshiftLeft,
|
||||||
}));
|
&ty,
|
||||||
|
&TypeKind::U64,
|
||||||
|
|scope, lhs, rhs| scope.block.build(Instr::ShiftLeft(lhs, rhs)).unwrap(),
|
||||||
|
));
|
||||||
}
|
}
|
||||||
for ty in INTEGERS.iter().chain(&[TypeKind::Bool, TypeKind::Char]) {
|
for ty in INTEGERS.iter().chain(&[TypeKind::Bool, TypeKind::Char]) {
|
||||||
intrinsics.push(boolean_binop_def(Cmp(CmpOperator::EQ), &ty, |scope, lhs, rhs| {
|
intrinsics.push(boolean_binop_def(Cmp(CmpOperator::EQ), &ty, |scope, lhs, rhs| {
|
||||||
@ -377,9 +386,7 @@ impl IntrinsicFunction for IntrinsicSizeOf {
|
|||||||
fn codegen<'ctx, 'a>(&self, scope: &mut Scope<'ctx, 'a>, _: &[StackValue]) -> Result<StackValue, ErrorKind> {
|
fn codegen<'ctx, 'a>(&self, scope: &mut Scope<'ctx, 'a>, _: &[StackValue]) -> Result<StackValue, ErrorKind> {
|
||||||
let instr = scope
|
let instr = scope
|
||||||
.block
|
.block
|
||||||
.build(Instr::Constant(reid_lib::ConstValueKind::U64(
|
.build(Instr::Constant(reid_lib::ConstValueKind::U64(self.0.size_of() / 8)))
|
||||||
self.0.size_of(&scope.type_map) / 8,
|
|
||||||
)))
|
|
||||||
.unwrap();
|
.unwrap();
|
||||||
Ok(StackValue(StackValueKind::Literal(instr), self.0.clone()))
|
Ok(StackValue(StackValueKind::Literal(instr), self.0.clone()))
|
||||||
}
|
}
|
||||||
@ -397,9 +404,7 @@ impl IntrinsicFunction for IntrinsicMalloc {
|
|||||||
|
|
||||||
let sizeof = scope
|
let sizeof = scope
|
||||||
.block
|
.block
|
||||||
.build(Instr::Constant(ConstValueKind::U64(
|
.build(Instr::Constant(ConstValueKind::U64(self.0.size_of() / 8)))
|
||||||
self.0.size_of(&scope.type_map) / 8,
|
|
||||||
)))
|
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let bytes = scope.block.build(Instr::Mul(sizeof, amount.instr())).unwrap();
|
let bytes = scope.block.build(Instr::Mul(sizeof, amount.instr())).unwrap();
|
||||||
let instr = scope.block.build(Instr::FunctionCall(function, vec![bytes])).unwrap();
|
let instr = scope.block.build(Instr::FunctionCall(function, vec![bytes])).unwrap();
|
||||||
|
@ -144,7 +144,6 @@ impl mir::Module {
|
|||||||
let mut types = HashMap::new();
|
let mut types = HashMap::new();
|
||||||
let mut type_values = HashMap::new();
|
let mut type_values = HashMap::new();
|
||||||
let mut debug_types = HashMap::new();
|
let mut debug_types = HashMap::new();
|
||||||
let mut type_map = HashMap::new();
|
|
||||||
|
|
||||||
macro_rules! insert_debug {
|
macro_rules! insert_debug {
|
||||||
($kind:expr) => {
|
($kind:expr) => {
|
||||||
@ -154,7 +153,8 @@ impl mir::Module {
|
|||||||
&compile_unit,
|
&compile_unit,
|
||||||
&debug,
|
&debug,
|
||||||
&debug_types,
|
&debug_types,
|
||||||
&type_map,
|
&type_values,
|
||||||
|
&types,
|
||||||
self.module_id,
|
self.module_id,
|
||||||
&self.tokens,
|
&self.tokens,
|
||||||
&modules,
|
&modules,
|
||||||
@ -182,8 +182,6 @@ impl mir::Module {
|
|||||||
|
|
||||||
for typedef in typedefs {
|
for typedef in typedefs {
|
||||||
let type_key = CustomTypeKey(typedef.name.clone(), typedef.source_module);
|
let type_key = CustomTypeKey(typedef.name.clone(), typedef.source_module);
|
||||||
type_map.insert(type_key.clone(), typedef.clone());
|
|
||||||
|
|
||||||
let type_value = match &typedef.kind {
|
let type_value = match &typedef.kind {
|
||||||
TypeDefinitionKind::Struct(StructType(fields)) => {
|
TypeDefinitionKind::Struct(StructType(fields)) => {
|
||||||
module.custom_type(CustomTypeKind::NamedStruct(NamedStruct(
|
module.custom_type(CustomTypeKind::NamedStruct(NamedStruct(
|
||||||
@ -200,7 +198,6 @@ impl mir::Module {
|
|||||||
};
|
};
|
||||||
types.insert(type_value, typedef.clone());
|
types.insert(type_value, typedef.clone());
|
||||||
type_values.insert(type_key.clone(), type_value);
|
type_values.insert(type_key.clone(), type_value);
|
||||||
|
|
||||||
insert_debug!(&TypeKind::CustomType(type_key.clone()));
|
insert_debug!(&TypeKind::CustomType(type_key.clone()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -383,7 +380,6 @@ impl mir::Module {
|
|||||||
functions: &functions,
|
functions: &functions,
|
||||||
types: &types,
|
types: &types,
|
||||||
type_values: &type_values,
|
type_values: &type_values,
|
||||||
type_map: &type_map,
|
|
||||||
globals: &globals,
|
globals: &globals,
|
||||||
stack_values: HashMap::new(),
|
stack_values: HashMap::new(),
|
||||||
debug: Some(Debug {
|
debug: Some(Debug {
|
||||||
@ -461,7 +457,6 @@ impl mir::Module {
|
|||||||
functions: &functions,
|
functions: &functions,
|
||||||
types: &types,
|
types: &types,
|
||||||
type_values: &type_values,
|
type_values: &type_values,
|
||||||
type_map: &type_map,
|
|
||||||
stack_values: HashMap::new(),
|
stack_values: HashMap::new(),
|
||||||
debug: Some(Debug {
|
debug: Some(Debug {
|
||||||
info: &debug,
|
info: &debug,
|
||||||
@ -523,7 +518,6 @@ impl mir::Module {
|
|||||||
functions: &functions,
|
functions: &functions,
|
||||||
types: &types,
|
types: &types,
|
||||||
type_values: &type_values,
|
type_values: &type_values,
|
||||||
type_map: &type_map,
|
|
||||||
stack_values: HashMap::new(),
|
stack_values: HashMap::new(),
|
||||||
debug: Some(Debug {
|
debug: Some(Debug {
|
||||||
info: &debug,
|
info: &debug,
|
||||||
@ -1317,47 +1311,45 @@ impl mir::Expression {
|
|||||||
if val.1 == *type_kind {
|
if val.1 == *type_kind {
|
||||||
Some(val)
|
Some(val)
|
||||||
} else {
|
} else {
|
||||||
let (ty, other) = if !state.should_load {
|
match (&val.1, type_kind) {
|
||||||
let TypeKind::CodegenPtr(inner) = &val.1 else {
|
(TypeKind::CodegenPtr(inner), TypeKind::UserPtr(ty2)) => match *inner.clone() {
|
||||||
panic!();
|
TypeKind::UserPtr(_) => Some(StackValue(
|
||||||
};
|
val.0.derive(
|
||||||
(*inner.clone(), TypeKind::CodegenPtr(Box::new(type_kind.clone())))
|
scope
|
||||||
} else {
|
.block
|
||||||
(val.1.clone(), type_kind.clone())
|
.build(Instr::BitCast(
|
||||||
};
|
val.instr(),
|
||||||
|
Type::Ptr(Box::new(type_kind.get_type(scope.type_values))),
|
||||||
dbg!(&ty, type_kind);
|
))
|
||||||
|
.unwrap(),
|
||||||
match (&ty, type_kind) {
|
),
|
||||||
(TypeKind::UserPtr(_), TypeKind::UserPtr(_)) => Some(StackValue(
|
TypeKind::CodegenPtr(Box::new(type_kind.clone())),
|
||||||
val.0.derive(
|
)),
|
||||||
scope
|
TypeKind::Borrow(ty1, _) => match *ty1.clone() {
|
||||||
.block
|
TypeKind::Array(ty1, _) => {
|
||||||
.build(Instr::BitCast(val.instr(), other.get_type(scope.type_values)))
|
if ty1 == *ty2 {
|
||||||
.unwrap(),
|
Some(StackValue(
|
||||||
),
|
val.0.derive(
|
||||||
other.clone(),
|
scope
|
||||||
)),
|
.block
|
||||||
(TypeKind::Borrow(ty1, _), TypeKind::UserPtr(ty2)) => {
|
.build(Instr::BitCast(
|
||||||
if let TypeKind::Array(ty1, _) = ty1.as_ref() {
|
val.instr(),
|
||||||
if ty1 == ty2 {
|
Type::Ptr(Box::new(type_kind.get_type(scope.type_values))),
|
||||||
Some(StackValue(
|
))
|
||||||
val.0.derive(
|
.unwrap(),
|
||||||
scope
|
),
|
||||||
.block
|
TypeKind::CodegenPtr(Box::new(type_kind.clone())),
|
||||||
.build(Instr::BitCast(val.instr(), other.get_type(scope.type_values)))
|
))
|
||||||
.unwrap(),
|
} else {
|
||||||
),
|
return Err(ErrorKind::Null);
|
||||||
other,
|
}
|
||||||
))
|
|
||||||
} else {
|
|
||||||
return Err(ErrorKind::Null).unwrap();
|
|
||||||
}
|
}
|
||||||
} else {
|
_ => return Err(ErrorKind::Null),
|
||||||
return Err(ErrorKind::Null).unwrap();
|
},
|
||||||
}
|
_ => panic!(),
|
||||||
}
|
},
|
||||||
(TypeKind::Char, TypeKind::U8)
|
(TypeKind::UserPtr(_), TypeKind::UserPtr(_))
|
||||||
|
| (TypeKind::Char, TypeKind::U8)
|
||||||
| (TypeKind::U8, TypeKind::Char)
|
| (TypeKind::U8, TypeKind::Char)
|
||||||
| (TypeKind::U8, TypeKind::I8) => Some(StackValue(
|
| (TypeKind::U8, TypeKind::I8) => Some(StackValue(
|
||||||
val.0.derive(
|
val.0.derive(
|
||||||
@ -1369,7 +1361,8 @@ impl mir::Expression {
|
|||||||
type_kind.clone(),
|
type_kind.clone(),
|
||||||
)),
|
)),
|
||||||
_ => {
|
_ => {
|
||||||
let cast_instr = ty
|
let cast_instr = val
|
||||||
|
.1
|
||||||
.get_type(scope.type_values)
|
.get_type(scope.type_values)
|
||||||
.cast_instruction(val.instr(), &type_kind.get_type(scope.type_values))
|
.cast_instruction(val.instr(), &type_kind.get_type(scope.type_values))
|
||||||
.unwrap();
|
.unwrap();
|
||||||
@ -1385,30 +1378,11 @@ impl mir::Expression {
|
|||||||
mir::ExprKind::AssociatedFunctionCall(ty, call) => codegen_function_call(Some(ty), call, scope, state)?,
|
mir::ExprKind::AssociatedFunctionCall(ty, call) => codegen_function_call(Some(ty), call, scope, state)?,
|
||||||
mir::ExprKind::GlobalRef(global_name, ty) => {
|
mir::ExprKind::GlobalRef(global_name, ty) => {
|
||||||
let global_value = scope.globals.get(global_name).unwrap();
|
let global_value = scope.globals.get(global_name).unwrap();
|
||||||
|
let a = Some(StackValue(
|
||||||
let value = scope.block.build(Instr::GetGlobal(global_value.clone())).unwrap();
|
StackValueKind::Literal(scope.block.build(Instr::GetGlobal(global_value.clone())).unwrap()),
|
||||||
|
ty.clone(),
|
||||||
if !state.should_load {
|
));
|
||||||
let allocated = scope
|
a
|
||||||
.block
|
|
||||||
.build(Instr::Alloca(ty.get_type(scope.type_values)))
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
scope
|
|
||||||
.block
|
|
||||||
.build(Instr::Store(allocated, value))
|
|
||||||
.unwrap()
|
|
||||||
.maybe_location(&mut scope.block, location.clone());
|
|
||||||
|
|
||||||
let a = Some(StackValue(
|
|
||||||
StackValueKind::Literal(allocated),
|
|
||||||
TypeKind::CodegenPtr(Box::new(ty.clone())),
|
|
||||||
));
|
|
||||||
a
|
|
||||||
} else {
|
|
||||||
let a = Some(StackValue(StackValueKind::Literal(value), ty.clone()));
|
|
||||||
a
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
if let Some(value) = &value {
|
if let Some(value) = &value {
|
||||||
|
@ -26,7 +26,6 @@ pub struct Scope<'ctx, 'scope> {
|
|||||||
pub(super) block: Block<'ctx>,
|
pub(super) block: Block<'ctx>,
|
||||||
pub(super) types: &'scope HashMap<TypeValue, TypeDefinition>,
|
pub(super) types: &'scope HashMap<TypeValue, TypeDefinition>,
|
||||||
pub(super) type_values: &'scope HashMap<CustomTypeKey, TypeValue>,
|
pub(super) type_values: &'scope HashMap<CustomTypeKey, TypeValue>,
|
||||||
pub(super) type_map: &'scope HashMap<CustomTypeKey, TypeDefinition>,
|
|
||||||
pub(super) assoc_functions: &'scope HashMap<AssociatedFunctionKey, ScopeFunctionKind<'ctx>>,
|
pub(super) assoc_functions: &'scope HashMap<AssociatedFunctionKey, ScopeFunctionKind<'ctx>>,
|
||||||
pub(super) functions: &'scope HashMap<String, ScopeFunctionKind<'ctx>>,
|
pub(super) functions: &'scope HashMap<String, ScopeFunctionKind<'ctx>>,
|
||||||
pub(super) binops: &'scope HashMap<BinopKey, StackBinopDefinition<'ctx>>,
|
pub(super) binops: &'scope HashMap<BinopKey, StackBinopDefinition<'ctx>>,
|
||||||
@ -50,7 +49,6 @@ impl<'ctx, 'a> Scope<'ctx, 'a> {
|
|||||||
functions: self.functions,
|
functions: self.functions,
|
||||||
types: self.types,
|
types: self.types,
|
||||||
type_values: self.type_values,
|
type_values: self.type_values,
|
||||||
type_map: self.type_map,
|
|
||||||
stack_values: self.stack_values.clone(),
|
stack_values: self.stack_values.clone(),
|
||||||
debug: self.debug.clone(),
|
debug: self.debug.clone(),
|
||||||
allocator: self.allocator.clone(),
|
allocator: self.allocator.clone(),
|
||||||
|
@ -109,7 +109,8 @@ impl TypeKind {
|
|||||||
&debug.scope,
|
&debug.scope,
|
||||||
debug.info,
|
debug.info,
|
||||||
debug.types,
|
debug.types,
|
||||||
scope.type_map,
|
scope.type_values,
|
||||||
|
scope.types,
|
||||||
scope.module_id,
|
scope.module_id,
|
||||||
scope.tokens,
|
scope.tokens,
|
||||||
scope.modules,
|
scope.modules,
|
||||||
@ -121,7 +122,8 @@ impl TypeKind {
|
|||||||
scope: &DebugScopeValue,
|
scope: &DebugScopeValue,
|
||||||
debug_info: &DebugInformation,
|
debug_info: &DebugInformation,
|
||||||
debug_types: &HashMap<TypeKind, DebugTypeValue>,
|
debug_types: &HashMap<TypeKind, DebugTypeValue>,
|
||||||
type_map: &HashMap<CustomTypeKey, TypeDefinition>,
|
type_values: &HashMap<CustomTypeKey, TypeValue>,
|
||||||
|
types: &HashMap<TypeValue, TypeDefinition>,
|
||||||
local_mod: SourceModuleId,
|
local_mod: SourceModuleId,
|
||||||
tokens: &Vec<FullToken>,
|
tokens: &Vec<FullToken>,
|
||||||
modules: &HashMap<SourceModuleId, ModuleCodegen>,
|
modules: &HashMap<SourceModuleId, ModuleCodegen>,
|
||||||
@ -140,12 +142,13 @@ impl TypeKind {
|
|||||||
scope,
|
scope,
|
||||||
debug_info,
|
debug_info,
|
||||||
debug_types,
|
debug_types,
|
||||||
type_map,
|
type_values,
|
||||||
|
types,
|
||||||
local_mod,
|
local_mod,
|
||||||
tokens,
|
tokens,
|
||||||
modules,
|
modules,
|
||||||
),
|
),
|
||||||
size_bits: self.size_of(type_map),
|
size_bits: self.size_of(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
TypeKind::Array(elem_ty, len) => {
|
TypeKind::Array(elem_ty, len) => {
|
||||||
@ -153,20 +156,21 @@ impl TypeKind {
|
|||||||
scope,
|
scope,
|
||||||
debug_info,
|
debug_info,
|
||||||
debug_types,
|
debug_types,
|
||||||
type_map,
|
type_values,
|
||||||
|
types,
|
||||||
local_mod,
|
local_mod,
|
||||||
tokens,
|
tokens,
|
||||||
modules,
|
modules,
|
||||||
);
|
);
|
||||||
DebugTypeData::Array(DebugArrayType {
|
DebugTypeData::Array(DebugArrayType {
|
||||||
size_bits: self.size_of(type_map),
|
size_bits: self.size_of(),
|
||||||
align_bits: self.alignment(),
|
align_bits: self.alignment(),
|
||||||
element_type: elem_ty,
|
element_type: elem_ty,
|
||||||
length: *len,
|
length: *len,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
TypeKind::CustomType(key) => {
|
TypeKind::CustomType(key) => {
|
||||||
let typedef = type_map.get(key).unwrap();
|
let typedef = types.get(type_values.get(key).unwrap()).unwrap();
|
||||||
match &typedef.kind {
|
match &typedef.kind {
|
||||||
TypeDefinitionKind::Struct(struct_type) => {
|
TypeDefinitionKind::Struct(struct_type) => {
|
||||||
let mut fields = Vec::new();
|
let mut fields = Vec::new();
|
||||||
@ -182,20 +186,21 @@ impl TypeKind {
|
|||||||
name: field.0.clone(),
|
name: field.0.clone(),
|
||||||
scope: scope.clone(),
|
scope: scope.clone(),
|
||||||
pos: location.map(|l| l.pos),
|
pos: location.map(|l| l.pos),
|
||||||
size_bits: field.1.size_of(type_map),
|
size_bits: field.1.size_of(),
|
||||||
offset: size_bits,
|
offset: size_bits,
|
||||||
flags: DwarfFlags,
|
flags: DwarfFlags,
|
||||||
ty: field.1.get_debug_type_hard(
|
ty: field.1.get_debug_type_hard(
|
||||||
scope,
|
scope,
|
||||||
debug_info,
|
debug_info,
|
||||||
debug_types,
|
debug_types,
|
||||||
type_map,
|
type_values,
|
||||||
|
types,
|
||||||
local_mod,
|
local_mod,
|
||||||
tokens,
|
tokens,
|
||||||
modules,
|
modules,
|
||||||
),
|
),
|
||||||
});
|
});
|
||||||
size_bits += field.1.size_of(type_map);
|
size_bits += field.1.size_of();
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
let location = if typedef.source_module != local_mod {
|
let location = if typedef.source_module != local_mod {
|
||||||
@ -217,7 +222,7 @@ impl TypeKind {
|
|||||||
}
|
}
|
||||||
_ => DebugTypeData::Basic(DebugBasicType {
|
_ => DebugTypeData::Basic(DebugBasicType {
|
||||||
name,
|
name,
|
||||||
size_bits: self.size_of(type_map),
|
size_bits: self.size_of(),
|
||||||
encoding: match self {
|
encoding: match self {
|
||||||
TypeKind::Bool => DwarfEncoding::Boolean,
|
TypeKind::Bool => DwarfEncoding::Boolean,
|
||||||
TypeKind::I8 => DwarfEncoding::SignedChar,
|
TypeKind::I8 => DwarfEncoding::SignedChar,
|
||||||
|
@ -1,5 +1,3 @@
|
|||||||
use reid_lib::builder::TypeValue;
|
|
||||||
|
|
||||||
use crate::util::maybe;
|
use crate::util::maybe;
|
||||||
|
|
||||||
use super::{typecheck::typerefs::TypeRefs, *};
|
use super::{typecheck::typerefs::TypeRefs, *};
|
||||||
@ -59,7 +57,7 @@ impl TypeKind {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn size_of(&self, map: &HashMap<CustomTypeKey, TypeDefinition>) -> u64 {
|
pub fn size_of(&self) -> u64 {
|
||||||
match self {
|
match self {
|
||||||
TypeKind::Bool => 1,
|
TypeKind::Bool => 1,
|
||||||
TypeKind::I8 => 8,
|
TypeKind::I8 => 8,
|
||||||
@ -74,16 +72,8 @@ impl TypeKind {
|
|||||||
TypeKind::U128 => 128,
|
TypeKind::U128 => 128,
|
||||||
TypeKind::Void => 0,
|
TypeKind::Void => 0,
|
||||||
TypeKind::Char => 8,
|
TypeKind::Char => 8,
|
||||||
TypeKind::Array(type_kind, len) => type_kind.size_of(map) * (*len as u64),
|
TypeKind::Array(type_kind, len) => type_kind.size_of() * (*len as u64),
|
||||||
TypeKind::CustomType(key) => match &map.get(key).unwrap().kind {
|
TypeKind::CustomType(..) => 32,
|
||||||
TypeDefinitionKind::Struct(struct_type) => {
|
|
||||||
let mut size = 0;
|
|
||||||
for field in &struct_type.0 {
|
|
||||||
size += field.1.size_of(map)
|
|
||||||
}
|
|
||||||
size
|
|
||||||
}
|
|
||||||
},
|
|
||||||
TypeKind::CodegenPtr(_) => 64,
|
TypeKind::CodegenPtr(_) => 64,
|
||||||
TypeKind::Vague(_) => panic!("Tried to sizeof a vague type!"),
|
TypeKind::Vague(_) => panic!("Tried to sizeof a vague type!"),
|
||||||
TypeKind::Borrow(..) => 64,
|
TypeKind::Borrow(..) => 64,
|
||||||
|
@ -118,13 +118,10 @@ impl mir::Expression {
|
|||||||
let mut globals = Vec::new();
|
let mut globals = Vec::new();
|
||||||
match &mut self.0 {
|
match &mut self.0 {
|
||||||
mir::ExprKind::FunctionCall(function_call) => {
|
mir::ExprKind::FunctionCall(function_call) => {
|
||||||
for param in &mut function_call.parameters {
|
|
||||||
globals.extend(param.gen_macros(data, state, map));
|
|
||||||
}
|
|
||||||
if function_call.is_macro {
|
if function_call.is_macro {
|
||||||
if let Some(existing_macro) = data.macros.get(&function_call.name) {
|
if let Some(existing_macro) = data.macros.get(&function_call.name) {
|
||||||
let mut literals = Vec::new();
|
let mut literals = Vec::new();
|
||||||
for param in &mut function_call.parameters {
|
for param in &function_call.parameters {
|
||||||
match ¶m.0 {
|
match ¶m.0 {
|
||||||
super::ExprKind::Literal(literal) => literals.push(literal.clone()),
|
super::ExprKind::Literal(literal) => literals.push(literal.clone()),
|
||||||
_ => state.note_errors(&vec![ErrorKind::InvalidMacroArgs], param.1),
|
_ => state.note_errors(&vec![ErrorKind::InvalidMacroArgs], param.1),
|
||||||
|
@ -6,12 +6,12 @@ use reid::{
|
|||||||
mir::{self},
|
mir::{self},
|
||||||
parse_module, perform_all_passes,
|
parse_module, perform_all_passes,
|
||||||
};
|
};
|
||||||
use reid_lib::{compile::CompileOutput, Context};
|
use reid_lib::Context;
|
||||||
use util::assert_err;
|
use util::assert_err;
|
||||||
|
|
||||||
mod util;
|
mod util;
|
||||||
|
|
||||||
fn test_compile(source: &str, name: &str) -> CompileOutput {
|
fn test(source: &str, name: &str, expected_exit_code: Option<i32>) {
|
||||||
assert_err(assert_err(std::panic::catch_unwind(|| {
|
assert_err(assert_err(std::panic::catch_unwind(|| {
|
||||||
let mut map = Default::default();
|
let mut map = Default::default();
|
||||||
let (id, tokens) = assert_err(parse_module(source, name, &mut map));
|
let (id, tokens) = assert_err(parse_module(source, name, &mut map));
|
||||||
@ -24,14 +24,7 @@ fn test_compile(source: &str, name: &str) -> CompileOutput {
|
|||||||
|
|
||||||
let codegen = assert_err(mir_context.codegen(&context));
|
let codegen = assert_err(mir_context.codegen(&context));
|
||||||
|
|
||||||
Ok::<_, ()>(codegen.compile(None, Vec::new()).output())
|
let output = codegen.compile(None, Vec::new()).output();
|
||||||
})))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn test(source: &str, name: &str, expected_exit_code: Option<i32>) {
|
|
||||||
assert_err(assert_err(std::panic::catch_unwind(|| {
|
|
||||||
let output = test_compile(source, name);
|
|
||||||
|
|
||||||
let time = SystemTime::now();
|
let time = SystemTime::now();
|
||||||
let in_path = PathBuf::from(format!(
|
let in_path = PathBuf::from(format!(
|
||||||
"/tmp/temp-{}.o",
|
"/tmp/temp-{}.o",
|
||||||
@ -164,8 +157,3 @@ fn associated_functions() {
|
|||||||
fn mutable_inner_functions() {
|
fn mutable_inner_functions() {
|
||||||
test(include_str!("../../examples/mutable_inner.reid"), "test", Some(0));
|
test(include_str!("../../examples/mutable_inner.reid"), "test", Some(0));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn cpu_raytracer_compiles() {
|
|
||||||
test_compile(include_str!("../../examples/cpu_raytracer.reid"), "test");
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user