From 141c881cf913a0785a6eb05c88896771cec4491e Mon Sep 17 00:00:00 2001 From: rvcas Date: Tue, 19 Jan 2021 21:14:00 -0500 Subject: [PATCH] feat: start to render some AST nodes --- editor/src/lang/expr.rs | 21 +++++++++++++++++++++ editor/src/lang/mod.rs | 2 +- editor/src/lib.rs | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 1 deletion(-) diff --git a/editor/src/lang/expr.rs b/editor/src/lang/expr.rs index d68ea5e724..55f2e06958 100644 --- a/editor/src/lang/expr.rs +++ b/editor/src/lang/expr.rs @@ -16,6 +16,10 @@ use roc_module::low_level::LowLevel; use roc_module::operator::CalledVia; use roc_module::symbol::{IdentIds, ModuleId, ModuleIds, Symbol}; use roc_parse::ast::StrLiteral; +use roc_parse::ast::{self, Attempting}; +use roc_parse::blankspace::space0_before; +use roc_parse::expr::expr; +use roc_parse::parser::{loc, Fail, Parser, State}; use roc_problem::can::{Problem, RuntimeError}; use roc_region::all::{Located, Region}; use roc_types::subs::{VarStore, Variable}; @@ -222,6 +226,23 @@ pub fn to_expr_id<'a>( (env.add(expr, region), output) } +pub fn str_to_expr2<'a>( + arena: &'a Bump, + input: &'a str, + env: &mut Env<'a>, + scope: &mut Scope, + region: Region, +) -> Result<(Expr2, self::Output), Fail> { + let state = State::new(input.trim().as_bytes(), Attempting::Module); + let parser = space0_before(loc(expr(0)), 0); + let answer = parser.parse(&arena, state); + + answer + .map(|(loc_expr, _)| loc_expr) + .map(|loc_expr| to_expr2(env, scope, &loc_expr.value, region)) + .map_err(|(fail, _)| fail) +} + pub fn to_expr2<'a>( env: &mut Env<'a>, scope: &mut Scope, diff --git a/editor/src/lang/mod.rs b/editor/src/lang/mod.rs index a4025ace23..61dadbcbd5 100644 --- a/editor/src/lang/mod.rs +++ b/editor/src/lang/mod.rs @@ -3,7 +3,7 @@ mod def; mod expr; mod module; mod pattern; -mod pool; +pub mod pool; pub mod roc_file; mod scope; mod types; diff --git a/editor/src/lib.rs b/editor/src/lib.rs index cfdf2c6eb2..1ef6f18eab 100644 --- a/editor/src/lib.rs +++ b/editor/src/lib.rs @@ -266,6 +266,8 @@ fn run_event_loop(file_path_opt: Option<&Path>) -> Result<(), Box> { ); } else { queue_no_file_text(&size, NOTHING_OPENED, CODE_TXT_XY.into(), &mut glyph_brush); + let ast = crate::lang::expr::str_to_expr2(); + render_node(&size, ast, CODE_TXT_XY.into(), &mut glyph_brush); } match draw_all_rects( @@ -364,6 +366,39 @@ fn begin_render_pass<'a>( }) } +fn render_node( + size: &PhysicalSize, + ast: Expr2, + position: Vector2, + glyph_brush: &mut GlyphBrush<()>, +) { + use Expr2::*; + + let area_bounds = (size.width as f32, size.height as f32).into(); + + match ast { + SmallInt { + number, + .. + // text, + // style, pretending always decimal for now + // var, + } => { + let code_text = Text { + position, + area_bounds, + color: CODE_COLOR.into(), + text: number.into(), + size: CODE_FONT_SIZE, + ..Default::default() + }; + + queue_code_text_draw(&code_text, glyph_brush); + } + _ => + }; +} + // returns bounding boxes for every glyph fn queue_editor_text( size: &PhysicalSize,