From accf123cd1c6368f77cd40355468acd0220f4d3f Mon Sep 17 00:00:00 2001 From: Roberto Alsina Date: Fri, 14 Jul 2023 11:51:03 -0300 Subject: [PATCH] Can't believe this works --- Makefile | 4 ++++ shortcodes.h | 3 ++- spec/shortcodes_spec.cr | 6 ++++-- src/shortcodes.cr | 36 +++++++++++++++++++++++++++++++----- 4 files changed, 41 insertions(+), 8 deletions(-) diff --git a/Makefile b/Makefile index e5e1f6d..f0e502c 100644 --- a/Makefile +++ b/Makefile @@ -10,4 +10,8 @@ test: tests.so cgreen-runner $^ debug: cgreen-debug tests.so +%o: %c + $(CC) -g -c -o $@ $^ +shortcodes.a: shortcodes.o + ar rcs $@ $^ .PHONY: test debug diff --git a/shortcodes.h b/shortcodes.h index 4b7336a..c662f56 100644 --- a/shortcodes.h +++ b/shortcodes.h @@ -5,7 +5,8 @@ // and goes on for len characters. struct chunk { - unsigned int start, len; + unsigned int start; + unsigned int len; }; typedef struct chunk chunk; diff --git a/spec/shortcodes_spec.cr b/spec/shortcodes_spec.cr index 7b9ad95..a95824c 100644 --- a/spec/shortcodes_spec.cr +++ b/spec/shortcodes_spec.cr @@ -1,9 +1,11 @@ require "./spec_helper" -describe Shortcodes do +describe "Shortcodes" do # TODO: Write tests it "works" do - false.should eq(true) + result = Shortcodes.parse("foo{{% bar %}}baz{{% /bar %}}qux"); + p! result.sccount; + p! result.shortcodes[0].matching; end end diff --git a/src/shortcodes.cr b/src/shortcodes.cr index 165d67a..1af0bc7 100644 --- a/src/shortcodes.cr +++ b/src/shortcodes.cr @@ -1,6 +1,32 @@ -# TODO: Write documentation for `Shortcodes` -module Shortcodes - VERSION = "0.1.0" +@[Link(ldflags: "#{__DIR__}/../shortcodes.o")] +lib Shortcodes - # TODO: Put your code here -end + struct Chunk + start : UInt32 + len : UInt32 + end + + struct ScError + position: UInt32 + code : UInt32 + end + + struct Shortcode + whole : Chunk + name : Chunk + data : Chunk + matching : LibC::Char + argnames : Chunk[100] + argvals : Chunk[100] + argcount : UInt32 + end + + struct ScResult + shortcodes : Shortcode[100] + sccount : UInt32 + errors : ScError[10] + errcount : UInt32 + end + + fun parse(input : Pointer(LibC::Char)) : ScResult; +end \ No newline at end of file