Skip to content

Commit

Permalink
Merge branch 'master' into feature/hexfloat
Browse files Browse the repository at this point in the history
  • Loading branch information
straight-shoota authored Dec 6, 2023
2 parents 64ae95e + 70095f4 commit 27b3131
Show file tree
Hide file tree
Showing 23 changed files with 282 additions and 111 deletions.
28 changes: 3 additions & 25 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -139,31 +139,9 @@ llvm_ext: $(LLVM_EXT_OBJ)
format: ## Format sources
./bin/crystal tool format$(if $(check), --check) src spec samples scripts

generate_data: ## Run generator scripts for Unicode, SSL config, ... (usually with `-B`/`--always-make` flag)

generate_data: spec/std/string/graphemes_break_spec.cr
spec/std/string/graphemes_break_spec.cr: scripts/generate_grapheme_break_specs.cr
$(CRYSTAL) run $<

generate_data: src/string/grapheme/properties.cr
src/string/grapheme/properties.cr: scripts/generate_grapheme_properties.cr
$(CRYSTAL) run $<

generate_data: src/openssl/ssl/defaults.cr
src/openssl/ssl/defaults.cr: scripts/generate_ssl_server_defaults.cr
$(CRYSTAL) run $<

generate_data: src/unicode/data.cr
src/unicode/data.cr: scripts/generate_unicode_data.cr
$(CRYSTAL) run $<

generate_data: src/crystal/system/win32/zone_names.cr
src/crystal/system/win32/zone_names.cr: scripts/generate_windows_zone_names.cr
$(CRYSTAL) run $<

generate_data: src/html/entities.cr
src/html/entities.cr: scripts/generate_html_entities.cr scripts/html_entities.ecr
$(CRYSTAL) run $<
.PHONY: generate_data
generate_data: ## Run generator scripts for Unicode, SSL config, ...
$(MAKE) -B -f scripts/generate_data.mk

.PHONY: install
install: $(O)/crystal man/crystal.1.gz ## Install the compiler at DESTDIR
Expand Down
30 changes: 4 additions & 26 deletions Makefile.win
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ static ?= ## Enable static linking
target ?= ## Cross-compilation target
interpreter ?= ## Enable interpreter feature
check ?= ## Enable only check when running format
order ?= ## Enable order for spec execution (values: "default" | "random" | seed number)
order ?=random ## Enable order for spec execution (values: "default" | "random" | seed number)

MAKEFLAGS += --no-builtin-rules
.SUFFIXES:
Expand Down Expand Up @@ -141,31 +141,9 @@ llvm_ext: $(LLVM_EXT_OBJ)
format: ## Format sources
.\bin\crystal tool format$(if $(check), --check) src spec samples scripts

generate_data: ## Run generator scripts for Unicode, SSL config, ... (usually with `-B`/`--always-make` flag)

generate_data: spec\std\string\graphemes_break_spec.cr
spec\std\string\graphemes_break_spec.cr: scripts\generate_grapheme_break_specs.cr
$(CRYSTAL) run $<

generate_data: src\string\grapheme\properties.cr
src\string\grapheme\properties.cr: scripts\generate_grapheme_properties.cr
$(CRYSTAL) run $<

generate_data: src\openssl\ssl\defaults.cr
src\openssl\ssl\defaults.cr: scripts\generate_ssl_server_defaults.cr
$(CRYSTAL) run $<

generate_data: src\unicode\data.cr
src\unicode\data.cr: scripts\generate_unicode_data.cr
$(CRYSTAL) run $<

generate_data: src\crystal\system\win32\zone_names.cr
src\crystal\system\win32\zone_names.cr: scripts\generate_windows_zone_names.cr
$(CRYSTAL) run $<

generate_data: src\html\entities.cr
src\html\entities.cr: scripts\generate_html_entities.cr scripts\html_entities.ecr
$(CRYSTAL) run $<
.PHONY: generate_data
generate_data: ## Run generator scripts for Unicode, SSL config, ...
$(MAKE) -B -f scripts/generate_data.mk

.PHONY: install
install: $(O)\crystal.exe ## Install the compiler at prefix
Expand Down
37 changes: 37 additions & 0 deletions scripts/generate_data.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

## Run all data generators
## $ make -f scripts/generate_data.mk

ifeq ($(OS),Windows_NT)
BIN_CRYSTAL=bin\crystal
else
BIN_CRYSTAL=bin/crystal
endif

.PHONY: all
all: ## Run all generators
$(BIN_CRYSTAL) run scripts/generate_grapheme_break_specs.cr
$(BIN_CRYSTAL) run scripts/generate_grapheme_properties.cr
$(BIN_CRYSTAL) run scripts/generate_ssl_server_defaults.cr
$(BIN_CRYSTAL) run scripts/generate_unicode_data.cr
$(BIN_CRYSTAL) run scripts/generate_windows_zone_names.cr
$(BIN_CRYSTAL) run scripts/generate_html_entities.cr

ifneq ($(OS),Windows_NT)
.PHONY: help
help: ## Show this help
@echo
@printf '\033[34mtargets:\033[0m\n'
@grep -hE '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) |\
sort |\
awk 'BEGIN {FS = ":.*?## "}; {printf " \033[36m%-15s\033[0m %s\n", $$1, $$2}'
@echo
@printf '\033[34moptional variables:\033[0m\n'
@grep -hE '^[a-zA-Z_-]+ \?=.*?## .*$$' $(MAKEFILE_LIST) |\
sort |\
awk 'BEGIN {FS = " \\?=.*?## "}; {printf " \033[36m%-15s\033[0m %s\n", $$1, $$2}'
@echo
@printf '\033[34mrecipes:\033[0m\n'
@grep -hE '^##.*$$' $(MAKEFILE_LIST) |\
awk 'BEGIN {FS = "## "}; /^## [a-zA-Z_-]/ {printf " \033[36m%s\033[0m\n", $$2}; /^## / {printf " %s\n", $$2}'
endif
12 changes: 12 additions & 0 deletions spec/compiler/interpreter/sizeof_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,16 @@ describe Crystal::Repl::Interpreter do
interpret("sizeof(typeof(1))").should eq(4)
end
end

context "instance_sizeof" do
it "interprets instance_sizeof typeof" do
interpret(<<-CRYSTAL).should eq(16)
class Foo
@x = 0_i64
end
instance_sizeof(typeof(Foo.new))
CRYSTAL
end
end
end
34 changes: 30 additions & 4 deletions spec/compiler/semantic/c_type_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,50 @@ require "../../spec_helper"

describe "Semantic: type" do
it "can call methods of original type" do
assert_type("
assert_type(<<-CRYSTAL, inject_primitives: true) { uint64 }
lib Lib
type X = Void*
fun foo : X
end
Lib.foo.address
", inject_primitives: true) { uint64 }
CRYSTAL
end

it "can call methods of parent type" do
assert_error("
assert_error(<<-CRYSTAL, "undefined method 'baz'")
lib Lib
type X = Void*
fun foo : X
end
Lib.foo.baz
", "undefined method 'baz'")
CRYSTAL
end

it "can access instance variables of original type" do
assert_type(<<-CRYSTAL) { int32 }
lib Lib
struct X
x : Int32
end
type Y = X
fun foo : Y
end
Lib.foo.@x
CRYSTAL
end

it "errors if original type doesn't support instance variables" do
assert_error(<<-CRYSTAL, "can't use instance variables inside primitive types (at Int32)")
lib Lib
type X = Int32
fun foo : X
end
Lib.foo.@x
CRYSTAL
end
end
28 changes: 28 additions & 0 deletions spec/std/big/big_int_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,34 @@ describe "BigInt" do
-5.to_big_i.remainder(-3).should eq(-2)
end

it "#bit" do
x = 123.to_big_i
x.bit(-10.to_big_i ** 99).should eq(0)
x.bit(-(2.to_big_i ** 64)).should eq(0)
x.bit(-1).should eq(0)
x.bit(0).should eq(1)
x.bit(2).should eq(0)
x.bit(3).should eq(1)
x.bit(6).should eq(1)
x.bit(7).should eq(0)
x.bit(64).should eq(0)
x.bit(2.to_big_i ** 64).should eq(0)
x.bit(10.to_big_i ** 99).should eq(0)

x = ~(123.to_big_i)
x.bit(-10.to_big_i ** 99).should eq(0)
x.bit(-(2.to_big_i ** 64)).should eq(0)
x.bit(-1).should eq(0)
x.bit(0).should eq(0)
x.bit(2).should eq(1)
x.bit(3).should eq(0)
x.bit(6).should eq(0)
x.bit(7).should eq(1)
x.bit(64).should eq(1)
x.bit(2.to_big_i ** 64).should eq(1)
x.bit(10.to_big_i ** 99).should eq(1)
end

it "does bitwise and" do
(123.to_big_i & 321).should eq(65)
(BigInt.new("96238761238973286532") & 86325735648).should eq(69124358272)
Expand Down
4 changes: 2 additions & 2 deletions spec/std/file_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -941,7 +941,7 @@ describe "File" do
pending! "Spec cannot run as superuser"
end
{% end %}
expect_raises(File::AccessDeniedError) { File.read(path) }
expect_raises(File::AccessDeniedError, "Error opening file with mode 'r': '#{path.inspect_unquoted}'") { File.read(path) }
end
end
{% end %}
Expand All @@ -958,7 +958,7 @@ describe "File" do
pending! "Spec cannot run as superuser"
end
{% end %}
expect_raises(File::AccessDeniedError) { File.write(path, "foo") }
expect_raises(File::AccessDeniedError, "Error opening file with mode 'w': '#{path.inspect_unquoted}'") { File.write(path, "foo") }
end
end

Expand Down
27 changes: 27 additions & 0 deletions spec/std/string_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2343,6 +2343,33 @@ describe "String" do
"foo".matches?(/bar/).should eq(false)
end

it "#matches_full?" do
pending! if {{ Regex::Engine.resolve.name == "Regex::PCRE" }}
"foo".matches_full?(/foo/).should be_true
"fooo".matches_full?(/foo/).should be_false
"ofoo".matches_full?(/foo/).should be_false
"pattern".matches_full?(/(\A)?pattern(\z)?/).should be_true
"_pattern_".matches_full?(/(\A)?pattern(\z)?/).should be_false
end

it "#match_full" do
pending! if {{ Regex::Engine.resolve.name == "Regex::PCRE" }}
"foo".match_full(/foo/).not_nil![0].should eq "foo"
"fooo".match_full(/foo/).should be_nil
"ofoo".match_full(/foo/).should be_nil
"pattern".match_full(/(\A)?pattern(\z)?/).not_nil![0].should eq "pattern"
"_pattern_".match_full(/(\A)?pattern(\z)?/).should be_nil
end

it "#match_full!" do
pending! if {{ Regex::Engine.resolve.name == "Regex::PCRE" }}
"foo".match_full!(/foo/).not_nil![0].should eq "foo"
expect_raises(Regex::Error) { "fooo".match_full!(/foo/) }
expect_raises(Regex::Error) { "ofoo".match_full!(/foo/) }
"pattern".match_full!(/(\A)?pattern(\z)?/).not_nil![0].should eq "pattern"
expect_raises(Regex::Error) { "_pattern_".match_full!(/(\A)?pattern(\z)?/) }
end

it "has size (same as size)" do
"テスト".size.should eq(3)
end
Expand Down
6 changes: 6 additions & 0 deletions src/big/big_int.cr
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,12 @@ struct BigInt < Int
BigInt.new { |mpz| LibGMP.com(mpz, self) }
end

def bit(bit : Int)
return 0 if bit < 0
return self < 0 ? 1 : 0 if bit > LibGMP::BitcntT::MAX
LibGMP.tstbit(self, LibGMP::BitcntT.new!(bit))
end

def &(other : BigInt) : BigInt
BigInt.new { |mpz| LibGMP.and(mpz, self, other) }
end
Expand Down
2 changes: 2 additions & 0 deletions src/big/lib_gmp.cr
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ lib LibGMP
fun xor = __gmpz_xor(rop : MPZ*, op1 : MPZ*, op2 : MPZ*)
fun com = __gmpz_com(rop : MPZ*, op : MPZ*)

fun tstbit = __gmpz_tstbit(op : MPZ*, bit_index : BitcntT) : Int

fun fdiv_q_2exp = __gmpz_fdiv_q_2exp(q : MPZ*, n : MPZ*, b : BitcntT)
fun mul_2exp = __gmpz_mul_2exp(rop : MPZ*, op1 : MPZ*, op2 : BitcntT)

Expand Down
29 changes: 10 additions & 19 deletions src/compiler/crystal/interpreter/compiler.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1422,6 +1422,14 @@ class Crystal::Repl::Compiler < Crystal::Visitor
false
end

def visit(node : InstanceSizeOf)
return false unless @wants_value

put_i32 inner_instance_sizeof_type(node.exp), node: node

false
end

def visit(node : TypeNode)
return false unless @wants_value

Expand Down Expand Up @@ -3361,25 +3369,8 @@ class Crystal::Repl::Compiler < Crystal::Visitor
@instructions.instructions.size
end

private def aligned_sizeof_type(node : ASTNode) : Int32
@context.aligned_sizeof_type(node)
end

private def aligned_sizeof_type(type : Type?) : Int32
@context.aligned_sizeof_type(type)
end

private def inner_sizeof_type(node : ASTNode) : Int32
@context.inner_sizeof_type(node)
end

private def inner_sizeof_type(type : Type?) : Int32
@context.inner_sizeof_type(type)
end

private def aligned_instance_sizeof_type(type : Type) : Int32
@context.aligned_instance_sizeof_type(type)
end
private delegate inner_sizeof_type, aligned_sizeof_type,
inner_instance_sizeof_type, aligned_instance_sizeof_type, to: @context

private def ivar_offset(type : Type, name : String) : Int32
if type.extern_union?
Expand Down
14 changes: 13 additions & 1 deletion src/compiler/crystal/interpreter/context.cr
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,19 @@ class Crystal::Repl::Context
end

def aligned_instance_sizeof_type(type : Type) : Int32
align(@program.instance_size_of(type.sizeof_type).to_i32)
align(inner_instance_sizeof_type(type))
end

def inner_instance_sizeof_type(node : ASTNode) : Int32
inner_instance_sizeof_type(node.type?)
end

def inner_instance_sizeof_type(type : Type) : Int32
@program.instance_size_of(type.sizeof_type).to_i32
end

def inner_instance_sizeof_type(type : Nil) : Int32
0
end

def offset_of(type : Type, index : Int32) : Int32
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/crystal/tools/doc/html/type.html
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ <h2>

<%= MethodSummaryTemplate.new("Constructors", type.constructors) %>
<%= MethodSummaryTemplate.new(type.program? ? "Method Summary" : "Class Method Summary", type.class_methods) %>
<%= MethodSummaryTemplate.new("Instance Method Summary", type.instance_methods) %>
<%= MethodSummaryTemplate.new("Macro Summary", type.macros) %>
<%= MethodSummaryTemplate.new("Instance Method Summary", type.instance_methods) %>

<div class="methods-inherited">
<% type.ancestors.each do |ancestor| %>
Expand All @@ -109,8 +109,8 @@ <h2>

<%= MethodDetailTemplate.new("Constructor Detail", type.constructors) %>
<%= MethodDetailTemplate.new(type.program? ? "Method Detail" : "Class Method Detail", type.class_methods) %>
<%= MethodDetailTemplate.new("Instance Method Detail", type.instance_methods) %>
<%= MethodDetailTemplate.new("Macro Detail", type.macros) %>
<%= MethodDetailTemplate.new("Instance Method Detail", type.instance_methods) %>
</div>

</body>
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/crystal/types.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2700,7 +2700,7 @@ module Crystal
end

delegate remove_typedef, pointer?, defs,
macros, reference_like?, parents, to: typedef
macros, reference_like?, parents, lookup_instance_var, to: typedef

def remove_indirection
self
Expand Down
Loading

0 comments on commit 27b3131

Please sign in to comment.