Skip to content
This repository has been archived by the owner on Jul 29, 2024. It is now read-only.

Commit

Permalink
<> as String -> String from <> (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
gavrilikhin-d authored Mar 30, 2024
1 parent fc9e0a5 commit 0f37868
Show file tree
Hide file tree
Showing 18 changed files with 98 additions and 101 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@
* [x] Don't define variables right away, when declaring them
* [x] Fix `let y = &x; y`
* [x] Fix definition of predeclared functions
* [x] `<:T> as String` -> `String from <:T>`. `as` should mean force cast (analog of `try_from().unwrap()`)
---
### Current task
* [ ] Sum of series benchmark
---
* [ ] Add type aliases
* [ ] Add dependency analysis for modules and declarations
* [ ] `<:T> as String` -> `String from <:T>`. `as` should mean force cast (analog of `try_from().unwrap()`)
* [ ] Add all `c` types
* [ ] Intern strings that are generated in IR
* [ ] `VariableReference` and `MemberReference` should have reference types
Expand Down
2 changes: 1 addition & 1 deletion examples/tea.ppl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn pour more water:
print "Pouring more water...\n"
sleep 2000 ms
currentLevel = maxLevel
print "New water level is: " + (currentLevel as String) + "mL\n\n"
print "New water level is: " + (String from currentLevel) + "mL\n\n"
sleep 1000 ms

fn set kettle on gas:
Expand Down
65 changes: 32 additions & 33 deletions src/ast_derive/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,45 +1,44 @@
use proc_macro::TokenStream;
use quote::quote;
use syn;

/// Auto-implement `FromStr` for AST node
///
/// AST node must implement `Parse` trait
#[proc_macro_derive(AST)]
pub fn ast_macro_derive(input: TokenStream) -> TokenStream {
let ast = syn::parse(input).unwrap();
let ast = syn::parse(input).unwrap();

impl_ast_macro(&ast)
impl_ast_macro(&ast)
}

fn impl_ast_macro(ast: &syn::DeriveInput) -> TokenStream {
let name = &ast.ident;
let gen = quote! {
impl std::str::FromStr for #name {
type Err = <#name as Parse>::Err;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let mut context = crate::syntax::Context::new(
crate::syntax::FullSourceLexer::new(s)
);

context.lexer.skip_spaces();

let res = #name::parse(&mut context);

let token = context.lexer.skip_spaces().next();
if token.is_some() {
return Err(
crate::syntax::error::ExtraToken {
token: token.unwrap(),
at: context.lexer.span().into()
}.into()
);
}
res
}
}
};

gen.into()
}
let name = &ast.ident;
let gen = quote! {
impl std::str::FromStr for #name {
type Err = <#name as Parse>::Err;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let mut context = crate::syntax::Context::new(
crate::syntax::FullSourceLexer::new(s)
);

context.lexer.skip_spaces();

let res = #name::parse(&mut context);

let token = context.lexer.skip_spaces().next();
if token.is_some() {
return Err(
crate::syntax::error::ExtraToken {
token: token.unwrap(),
at: context.lexer.span().into()
}.into()
);
}
res
}
}
};

gen.into()
}
2 changes: 1 addition & 1 deletion src/hir/declarations/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ mod tests {
use super::*;
use crate::ast;
use crate::compilation::Compiler;
use crate::hir::{GenericType, Member, Type};
use crate::hir::GenericType;
use crate::semantics::{ModuleContext, ToHIR};
use pretty_assertions::assert_eq;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ define void @"println <:Type<A>>"(%"Type<A>" %0) !dbg !15 {
%x = alloca %"Type<A>", align 8
store %"Type<A>" %0, ptr %x, align 8
%2 = load %"Type<A>", ptr %x, align 8, !dbg !16
%3 = call %String @"<:Type<A>> as String"(%"Type<A>" %2), !dbg !16
%3 = call %String @"String from <:Type<A>>"(%"Type<A>" %2), !dbg !16
call void @"println <:String>"(%String %3), !dbg !16
br label %return, !dbg !16

Expand All @@ -72,7 +72,7 @@ return: ; preds = %1

declare void @"println <:String>"(%String)

define %String @"<:Type<A>> as String"(%"Type<A>" %0) !dbg !17 {
define %String @"String from <:Type<A>>"(%"Type<A>" %0) !dbg !17 {
%return_value = alloca %String, align 8
%ty = alloca %"Type<A>", align 8
store %"Type<A>" %0, ptr %ty, align 8
Expand Down Expand Up @@ -117,7 +117,7 @@ return: ; preds = %1
!14 = !DILocation(line: 2, column: 19, scope: !11)
!15 = distinct !DISubprogram(name: "println <:Type<A>>", linkageName: "println <:Type<A>>", scope: !11, file: !2, line: 2, type: !4, spFlags: DISPFlagDefinition, unit: !1)
!16 = !DILocation(line: 2, column: 21, scope: !15)
!17 = distinct !DISubprogram(name: "<:Type<A>> as String", linkageName: "<:Type<A>> as String", scope: !15, file: !2, line: 2, type: !4, spFlags: DISPFlagDefinition, unit: !1)
!17 = distinct !DISubprogram(name: "String from <:Type<A>>", linkageName: "String from <:Type<A>>", scope: !15, file: !2, line: 2, type: !4, spFlags: DISPFlagDefinition, unit: !1)
!18 = !DILocation(line: 2, column: 21, scope: !17)
!19 = distinct !DISubprogram(name: "type of <:A>", linkageName: "type of <:A>", scope: !11, file: !2, line: 2, type: !4, spFlags: DISPFlagDefinition, unit: !1)
!20 = !DILocation(line: 2, column: 21, scope: !19)
6 changes: 3 additions & 3 deletions src/ir/tests/snapshots/ppl__ir__tests__type_of.ir.snap
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ define void @"println <:Type<Integer>>"(%"Type<Integer>" %0) !dbg !17 {
%x = alloca %"Type<Integer>", align 8
store %"Type<Integer>" %0, ptr %x, align 8
%2 = load %"Type<Integer>", ptr %x, align 8, !dbg !18
%3 = call %String @"<:Type<Integer>> as String"(%"Type<Integer>" %2), !dbg !18
%3 = call %String @"String from <:Type<Integer>>"(%"Type<Integer>" %2), !dbg !18
call void @"println <:String>"(%String %3), !dbg !18
br label %return, !dbg !18

Expand All @@ -83,7 +83,7 @@ return: ; preds = %1

declare void @"println <:String>"(%String)

define %String @"<:Type<Integer>> as String"(%"Type<Integer>" %0) !dbg !19 {
define %String @"String from <:Type<Integer>>"(%"Type<Integer>" %0) !dbg !19 {
%return_value = alloca %String, align 8
%ty = alloca %"Type<Integer>", align 8
store %"Type<Integer>" %0, ptr %ty, align 8
Expand Down Expand Up @@ -119,5 +119,5 @@ return: ; preds = %1
!16 = !DILocation(line: 1, column: 8, scope: !13)
!17 = distinct !DISubprogram(name: "println <:Type<Integer>>", linkageName: "println <:Type<Integer>>", scope: !13, file: !2, line: 1, type: !4, spFlags: DISPFlagDefinition, unit: !1)
!18 = !DILocation(line: 1, column: 10, scope: !17)
!19 = distinct !DISubprogram(name: "<:Type<Integer>> as String", linkageName: "<:Type<Integer>> as String", scope: !17, file: !2, line: 1, type: !4, spFlags: DISPFlagDefinition, unit: !1)
!19 = distinct !DISubprogram(name: "String from <:Type<Integer>>", linkageName: "String from <:Type<Integer>>", scope: !17, file: !2, line: 1, type: !4, spFlags: DISPFlagDefinition, unit: !1)
!20 = !DILocation(line: 1, column: 10, scope: !19)
22 changes: 11 additions & 11 deletions src/runtime/ppl.ppl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type Type<T>:
type None

/// Convert `None` to `String`
fn <:None> as String => "none"
fn String from <:None> => "none"
//---------------------------------


Expand Down Expand Up @@ -57,7 +57,7 @@ fn <x: Bool> or <y: Bool> -> Bool:
return y

/// Convert `Bool` to `String`
fn <x: Bool> as String -> String:
fn String from <x: Bool> -> String:
if x:
return "true"
return "false"
Expand Down Expand Up @@ -119,7 +119,7 @@ fn <x: Integer> % <y: Integer> -> Integer

/// Convert `Integer` to `String`
@mangle_as("integer_as_string")
fn <:Integer> as String -> String
fn String from <:Integer> -> String

@mangle_as("destroy_integer")
fn destroy <:Integer>
Expand Down Expand Up @@ -162,7 +162,7 @@ fn <:Rational> / <:Rational> -> Rational

/// Convert `Rational` to `String`
@mangle_as("rational_as_string")
fn <:Rational> as String -> String
fn String from <:Rational> -> String

@mangle_as("destroy_rational")
fn destroy <:Rational>
Expand Down Expand Up @@ -203,13 +203,13 @@ fn clone <:String> -> String
//=================================
/// Trait for things that may be interpreted as string
trait AsString:
fn <:Self> as String -> String
fn String from <:Self> -> String

/// Print anything that may be interpreted as String
fn print <x: AsString> => print (x as String)
fn print <x: AsString> => print (String from x)

/// Print anything that may be interpreted as String with newline
fn println <x: AsString> => println (x as String)
fn println <x: AsString> => println (String from x)
//---------------------------------

//=================================
Expand Down Expand Up @@ -240,7 +240,7 @@ trait Ord:
// Type
//=================================
/// Name of a type including generic parameters
fn<T> <ty: Type<T>> as String => ty.name
fn<T> String from <ty: Type<T>> => ty.name

/// Size of a type in bytes
fn<T> size of <ty: Type<T>> => ty.size
Expand All @@ -266,7 +266,7 @@ fn <address: MemoryAddress> + <offset: Integer> -> MemoryAddress:

/// Convert `MemoryAddress` to `String`
@mangle_as("memory_address_as_string")
fn <address: MemoryAddress> as String -> String
fn String from <address: MemoryAddress> -> String

/// Allocate `n` bytes of memory.
/// Warning: memory is uninitialized!
Expand Down Expand Up @@ -381,7 +381,7 @@ fn <x: I32> - <y: I32> => x + -y

/// Convert `I32` to `String`
@mangle_as("i32_as_string")
fn <:I32> as String -> String
fn String from <:I32> -> String

/// Convert `I32` to `Integer`
@mangle_as("integer_from_i32")
Expand Down Expand Up @@ -413,7 +413,7 @@ fn <:F64> * <:F64> -> F64

/// Convert `F64` to `String`
@mangle_as("f64_as_string")
fn <:F64> as String -> String
fn String from <:F64> -> String

/// Convert `Rational` to `F64`
@mangle_as("f64_from_rational")
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/src/f64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type F64 = f64;
/// ```no_run
/// /// Convert `F64` to `String`
/// @mangle_as("f64_as_string")
/// fn <:F64> as String -> String
/// fn String from <:F64> -> String
/// ```
#[no_mangle]
pub extern "C" fn f64_as_string(d: F64) -> String {
Expand Down
8 changes: 4 additions & 4 deletions src/runtime/src/integer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ pub extern "C" fn integer_from_c_string(str: *const i8) -> Integer {

/// Converts `Integer` to `String`
///
/// Runtime for builtin ppl's function:
/// ```ppl
/// fn <:Integer> as String -> String
/// # PPL
/// ```no_run
/// fn String from <:Integer> -> String
/// ```
#[no_mangle]
pub extern "C" fn integer_as_string(i: Integer) -> String {
Expand Down Expand Up @@ -266,7 +266,7 @@ pub extern "C" fn i32_plus_i32(x: i32, y: i32) -> i32 {
/// # PPL
/// ```no_run
/// @mangle_as("i32_as_string")
/// fn <:I32> as String -> String
/// fn String from <:I32> -> String
/// ```
#[no_mangle]
pub extern "C" fn i32_as_string(x: i32) -> String {
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub struct MemoryAddress {

/// # PPL
/// ```no_run
/// fn <address: MemoryAddress> as String -> String
/// fn String from <address: MemoryAddress> -> String
/// ```
#[no_mangle]
pub extern "C" fn memory_address_as_string(address: MemoryAddress) -> String {
Expand Down
8 changes: 3 additions & 5 deletions src/runtime/src/rational.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,9 @@ pub extern "C" fn rational_from_c_string(str: *const i8) -> Rational {
}
}

/// Converts `Rational` to `String`
///
/// Runtime for builtin ppl's function:
/// ```ppl
/// fn <:Rational> as String -> String
/// # PPL
/// ```no_run
/// fn String from <:Rational> -> String
/// ```
#[no_mangle]
pub extern "C" fn rational_as_string(r: Rational) -> String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ define void @"println <:Bool>"(i1 %0) !dbg !22 {
%x = alloca i1, align 1
store i1 %0, ptr %x, align 1
%2 = load i1, ptr %x, align 1, !dbg !23
%3 = call %String @"<:Bool> as String"(i1 %2), !dbg !23
%3 = call %String @"String from <:Bool>"(i1 %2), !dbg !23
call void @"println <:String>"(%String %3), !dbg !23
br label %return, !dbg !23

Expand All @@ -95,7 +95,7 @@ return: ; preds = %1

declare void @"println <:String>"(%String)

define %String @"<:Bool> as String"(i1 %0) !dbg !24 {
define %String @"String from <:Bool>"(i1 %0) !dbg !24 {
%return_value = alloca %String, align 8
%x = alloca i1, align 1
store i1 %0, ptr %x, align 1
Expand Down Expand Up @@ -192,7 +192,7 @@ return: ; preds = %1
!21 = !DILocation(line: 12, column: 20, scope: !12)
!22 = distinct !DISubprogram(name: "println <:Bool>", linkageName: "println <:Bool>", scope: !12, file: !2, line: 13, type: !4, spFlags: DISPFlagDefinition, unit: !1)
!23 = !DILocation(line: 13, scope: !22)
!24 = distinct !DISubprogram(name: "<:Bool> as String", linkageName: "<:Bool> as String", scope: !22, file: !2, line: 13, type: !4, spFlags: DISPFlagDefinition, unit: !1)
!24 = distinct !DISubprogram(name: "String from <:Bool>", linkageName: "String from <:Bool>", scope: !22, file: !2, line: 13, type: !4, spFlags: DISPFlagDefinition, unit: !1)
!25 = !DILocation(line: 13, scope: !24)
!26 = distinct !DISubprogram(name: "id <:Integer>", linkageName: "id <:Integer>", scope: !12, file: !2, line: 8, type: !4, spFlags: DISPFlagDefinition, unit: !1)
!27 = !DILocation(line: 8, column: 19, scope: !26)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ return: ; preds = %0
}

define void @"println <:None>"() !dbg !9 {
%1 = call %String @"<:None> as String"(), !dbg !10
%1 = call %String @"String from <:None>"(), !dbg !10
call void @"println <:String>"(%String %1), !dbg !10
br label %return, !dbg !10

Expand All @@ -32,7 +32,7 @@ return: ; preds = %0

declare void @"println <:String>"(%String)

define %String @"<:None> as String"() !dbg !11 {
define %String @"String from <:None>"() !dbg !11 {
%return_value = alloca %String, align 8
%1 = call %String @string_from_c_string_and_length(ptr @0, i64 4), !dbg !12
store %String %1, ptr %return_value, align 8, !dbg !12
Expand Down Expand Up @@ -77,7 +77,7 @@ declare %Integer @"do something"()
!8 = !DILocation(line: 3, column: 9, scope: !3)
!9 = distinct !DISubprogram(name: "println <:None>", linkageName: "println <:None>", scope: !3, file: !2, line: 3, type: !4, spFlags: DISPFlagDefinition, unit: !1)
!10 = !DILocation(line: 3, column: 22, scope: !9)
!11 = distinct !DISubprogram(name: "<:None> as String", linkageName: "<:None> as String", scope: !9, file: !2, line: 3, type: !4, spFlags: DISPFlagDefinition, unit: !1)
!11 = distinct !DISubprogram(name: "String from <:None>", linkageName: "String from <:None>", scope: !9, file: !2, line: 3, type: !4, spFlags: DISPFlagDefinition, unit: !1)
!12 = !DILocation(line: 3, column: 22, scope: !11)
!13 = distinct !DISubprogram(name: "println <:Integer>", linkageName: "println <:Integer>", scope: !3, file: !2, line: 3, type: !4, spFlags: DISPFlagDefinition, unit: !1)
!14 = !DILocation(line: 3, column: 22, scope: !13)
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ define void @"println <:Bool>"(i1 %0) !dbg !17 {
%x = alloca i1, align 1
store i1 %0, ptr %x, align 1
%2 = load i1, ptr %x, align 1, !dbg !18
%3 = call %String @"<:Bool> as String"(i1 %2), !dbg !18
%3 = call %String @"String from <:Bool>"(i1 %2), !dbg !18
call void @"println <:String>"(%String %3), !dbg !18
br label %return, !dbg !18

Expand All @@ -76,7 +76,7 @@ return: ; preds = %1

declare void @"println <:String>"(%String)

define %String @"<:Bool> as String"(i1 %0) !dbg !19 {
define %String @"String from <:Bool>"(i1 %0) !dbg !19 {
%return_value = alloca %String, align 8
%x = alloca i1, align 1
store i1 %0, ptr %x, align 1
Expand Down Expand Up @@ -127,5 +127,5 @@ declare i1 @integer_eq_integer(%Integer, %Integer)
!16 = !DILocation(line: 5, column: 13, scope: !12)
!17 = distinct !DISubprogram(name: "println <:Bool>", linkageName: "println <:Bool>", scope: !12, file: !2, line: 5, type: !4, spFlags: DISPFlagDefinition, unit: !1)
!18 = !DILocation(line: 5, column: 14, scope: !17)
!19 = distinct !DISubprogram(name: "<:Bool> as String", linkageName: "<:Bool> as String", scope: !17, file: !2, line: 5, type: !4, spFlags: DISPFlagDefinition, unit: !1)
!19 = distinct !DISubprogram(name: "String from <:Bool>", linkageName: "String from <:Bool>", scope: !17, file: !2, line: 5, type: !4, spFlags: DISPFlagDefinition, unit: !1)
!20 = !DILocation(line: 5, column: 14, scope: !19)
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ let x: Type<Integer> = (Type<Integer>:Type<Integer>)
let y: Integer = clone (Type<Integer>:Type<Integer>).size
println size of (Type<None>:Type<None>) == 0
println size of (Type<Bool>:Type<Bool>) == 1
println (Type<None>:Type<None>) as String
println (Type<Type<Integer>>:Type<Type<Integer>>) as String
println String from (Type<None>:Type<None>)
println String from (Type<Type<Integer>>:Type<Type<Integer>>)
println clone (y:Integer) == size of (Type<Integer>:Type<Integer>)
println (Type<Integer>:Type<Integer>)
Loading

0 comments on commit 0f37868

Please sign in to comment.